Date.prototype.add = Date.prototype.add || function (date,time) {
var _time = time.split(' ');
var num = parseInt(_time[0]);
var type = _time[1];
var result = null;
switch (type){
case 'year':
result = new Date(date.getFullYear()+num,date.getMonth(),date.getDate(),date.getHours(),date.getMinutes(),date.getSeconds());
break;
case 'month':
result = new Date(date.getFullYear(),date.getMonth()+num,date.getDate(),date.getHours(),date.getMinutes(),date.getSeconds());
break;
case 'week':
result = new Date(date.getTime()+(1000 * 60 * 60 * 24 * 7 * num));
break;
case 'day':
result = new Date(date.getTime()+(1000 * 60 * 60 * 24 * num));
break;
case 'hours':
result = new Date(date.getTime()+(1000 * 60 * 60 * num));
break;
case 'minute':
result = new Date(date.getTime()+(1000 * 60 * num));
break;
case 'second':
result = new Date(date.getTime()+(1000 * num));
break;
}
return {
year:result.getFullYear(),
month:result.getMonth()+1,
day:result.getDate(),
hour:result.getHours(),
minute:result.getMinutes(),
second:result.getSeconds()
};
};
//测试数据
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate();
/*
单位 hours minute second day week month year
*/
console.log(date.add(date,'+1 day'));
console.log('now is',year,month,day);
js 日期加减
最新推荐文章于 2017-01-31 00:39:59 发布