1.首先把String转为日期:
var str ='2012-08-12 23:13:15';
str = str.replace(/-/g,"/");
var date = new Date(str );
2.进行算术运算
需要注意一点是days参数需要转int,否则直接会拼接字符串
function addDate(date, days) {
if (days == undefined || days == '') {
days = 1;
}
var date = new Date(date);
date.setDate(date.getDate() + parseInt(days));
var month = date.getMonth() + 1;
var day = date.getDate();
return date.getFullYear() + '-' + getFormatDate(month) + '-' + getFormatDate(day);
}
// 日期月份/天的显示,如果是1位数,则在前面加上'0'
function getFormatDate(arg) {
if (arg == undefined || arg == '') {
return '';
}
var re = arg + '';
if (re.length < 2) {
re = '0' + re;
}
return re;
}