Date.prototype.Format = function (fmt) { //author: meizz   
    var o = {  
        "M+": this.getMonth()+1 , //月份   
        "d+": this.getDate(), //日   
        "h+": this.getHours(), //小时   
        "m+": this.getMinutes(), //分   
        "s+": this.getSeconds(), //秒   
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度   
        "S": this.getMilliseconds() //毫秒   
    };  
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
    for (var k in o)  
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));  
    return fmt;  
}  
   var date=new Date;
 var year=date.getFullYear(); 
 var month=date.getMonth()+1;
 month =(month<10 ? "0"+month:month); 
var half_month = (year.toString()+month.toString()-7);//6个月前
var month = (year.toString()+month.toString()-1);//上月
var yday=new Date(new Date().getTime()-1*24*60*60*1000).Format("yyyy-MM-dd");//昨天
var tday=new Date(new Date().getTime()).Format("yyyy-MM-dd");//今天 
这是我的kettle里的js写的,但为什么翻年了 就变成201800了,6个月期前也不对,求大神

解决方案 »

  1.   

    Date.prototype.addDays = function (d) {
        return new Date(this.valueOf() + d * 24 * 60 * 60 * 1000);
    }
    alert(new Date().addDays(7));
    alert(new Date().addDays(-1));按这个思路你自己扩展一下
      

  2.   

    你那些日期基本上都是字符串和数字之间的运算当然会出错;我试了一下这样应该可以:
     var date=new Date();
     date.setMonth(date.getMonth()-6);//6个月前
     console.log(date);
     var Month = date.getMonth();
     Month = (Month+1<10)?"0"+Month:Month;
     var year = date.getFullYear();
     var half_Month  = year+""+Month;
     console.log(half_Month);  
      

  3.   

    ReferenceError: "console" is not defined. (script#33) 为什么用你的在kettle里要报错
      

  4.   

    直接在开发工具上是不认识console的(我用的eclipse)。你在网页上试一下。而且console.log()只是一个输出语句,可以不要,或者换为alert()试一下。(这回复验证码我是真的服了)
      

  5.   

    其实js对日期的计算非常有意思,弱类型到不能再弱类型了,比如,声明2018年1月1日,可以使用 new Date(2018,0,1),如果声明2017年12月31日,则可以new Date(2017,11,31),也可以new Date(2018,0,0),还可以new Date(2018,-1,31),也就是说js会自动将你声明的时间转换成合法时间那么接下来就简单了
    var d = new Date(); // 获取当前时间
    var yesterday = new Date(d.getFullYear(),d.getMonth(),d.getDate()-1); // 日期位减1,表示前一天
    var half_years_ago = new Date(d.getFullYear(),d.getMonth()-6,d.getDate()); // 月份位减6,表示六个月前
      

  6.   


    window.console = window.console || {log:function(s){alert(s);}}; Date.prototype.calcAndFormat = function(num,type,format){
    var m = this.getMonth();
    var d = this.getDate(); if(type == 1){//天
    this.setDate(d + num);
    }else if(type == 2){//月
    this.setMonth(m + num)
    } var o = {  
            "M+": this.getMonth()+1 , //月份   
            "d+": this.getDate(), //日   
            "h+": this.getHours(), //小时   
            "m+": this.getMinutes(), //分   
            "s+": this.getSeconds(), //秒     
            "S": this.getMilliseconds() //毫秒   
        };  
        if (/(y+)/.test(format)) 
         format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
        for (var k in o)  
         if (new RegExp("(" + k + ")").test(format)) 
         format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('0'+o[k]).slice(-2));  
        
        return format;   
    } var D = new Date();
    var format = D.calcAndFormat(-1,1,'yyyy-MM-dd');
    console.log(format);