使用new data计算时间以及格式转换

1、时间计算,往后加30(默认一个月的时间),sxTime表示的是在当前时间往后加几天的之后一个月

function maxDate1(){
            var nowDate = new Date();
            maxDate = new Date(nowDate.setDate(nowDate.getDate()+(30+sxTime)));
            return maxDate;
        }

2、个格式转换,用封装好的js进行转换,转入的是new data对象才可以

var nowDate = new Date();//今天
        var selectDate = new MobileSelectDate();
        var tbMinDate = new Date(nowDate.setDate(nowDate.getDate()+1)); //次日
        var tbMaxDate = new Date(nowDate.setDate(nowDate.getDate()+29));
        var tbMinDate = tbMinDate.format("yyyy/MM/dd");//转换格式
        var tbMaxDate = tbMaxDate.format("yyyy/MM/dd");//转换格式

/***
功能:format:格式化时间。
用法:
yourdate.format("你的日期格式");
例子:
    obj0 = new Date("Sun May 04 2008").format("yyyy-MM-dd");
    obj1 = new Date().format("yyyy-MM-dd hh:mm:ss");
    obj2 = new Date().format("yyyy-MM-dd");
    obj3 = new Date().format("yyyy/MM/dd");
    obj4 = new Date().format("MM/dd/yyyy");
*****/
Date.prototype.format = function(format){
   var o = {
     "M+" : this.getMonth()+1, //month
     "d+" : this.getDate(),    //day
     "h+" : this.getHours(),   //hour
     "m+" : this.getMinutes(), //minute
     "s+" : this.getSeconds(), //second
     "q+" : Math.floor((this.getMonth()+3)/3), //quarter
     "S" : this.getMilliseconds() //millisecond
   }
    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] : ("00"+ o[k]).substr((""+ o[k]).length));
        }
    }
    return format;
}

 3、使用new date的时候如果公用一个new date那么其中一个修改会影响到另外一个跟着变

应用场景:时间次日生效,或者一个月(30),

// 设置生效时间和终止时间
        var nowDate = new Date();//今天
        var tbMinDate = new Date(nowDate.setDate(nowDate.getDate()+1)); //次日
        var tbMaxDate = new Date(nowDate.setDate(nowDate.getDate()+29)); //nowDate已经被多加一天了,因为nowDate指向的是同一个对象,前面已被修改
var tbMinDate = tbMinDate.format("yyyy年MM月dd日 00:00:00");//转换格式  var tbMaxDate = tbMaxDate.format("yyyy年MM月dd日 23:59:59");//转换格式 
原文地址:https://www.cnblogs.com/qdlhj/p/10418109.html