var bplimitdt = "${pd.LimitDT}"; //选中系统到期时间
bplimitdt = new Date(bplimitdt); //字符串转化成时间
var newDate = DateAdd("m ", 3, bplimitdt); //bpmonth:新增三个月份
newDate = formatDate(newDate.getTime(), 'YY-MM-DD hh:mm:ss'); //格式化时间
格式化时间
function formatDate(time, format) {
var date = new Date(time);
var year = date.getFullYear(),
month = date.getMonth() + 1,
//月份是从0开始的
day = date.getDate(),
hour = date.getHours(),
min = date.getMinutes(),
sec = date.getSeconds();
var preArr = Array.apply(null, Array(10)).map(function(elem, index) {
return '0' + index;
});开个长度为10的数组格式为00 01 02 03
var newTime = format.replace(/YY/g, year).replace(/MM/g, preArr[month] || month).replace(/DD/g, preArr[day] || day).replace(/hh/g, preArr[hour] || hour).replace(/mm/g, preArr[min] || min).replace(/ss/g, preArr[sec] || sec);
return newTime;
}
日期增加运算
function DateAdd(interval, number, date) {
/*
* 功能:实现VBScript的DateAdd功能.
* 参数:interval,字符串表达式,表示要添加的时间间隔.
* 参数:number,数值表达式,表示要添加的时间间隔的个数.
* 参数:date,时间对象.
* 返回:新的时间对象.
* var now = new Date();
* var newDate = DateAdd( "d ",5,now);
*--------------- DateAdd(interval,number,date) -----------------
*/
switch (interval) {
case "y ":
{
date.setFullYear(date.getFullYear() + number);
return date;
break;
}
case "q ":
{
date.setMonth(date.getMonth() + number * 3);
return date;
break;
}
case "m ":
{
date.setMonth(date.getMonth() + number);
return date;
break;
}
case "w ":
{
date.setDate(date.getDate() + number * 7);
return date;
break;
}
case "d ":
{
date.setDate(date.getDate() + number);
return date;
break;
}
case "h ":
{
date.setHours(date.getHours() + number);
return date;
break;
}
case "m ":
{
date.setMinutes(date.getMinutes() + number);
return date;
break;
}
case "s ":
{
date.setSeconds(date.getSeconds() + number);
return date;
break;
}
default:
{
date.setDate(d.getDate() + number);
return date;
break;
}
}
}
var now = new Date();
//加五天.
var newDate = DateAdd("d ", 5, now);
alert(newDate.toLocaleDateString())
//加两个月.
newDate = DateAdd("m ", 2, now);
alert(newDate.toLocaleDateString())
//加一年
newDate = DateAdd("y ", 1, now);
alert(newDate.toLocaleDateString())