JavaScript中如何对时间进行+1
先获取指定格式的当前时间
var myDate = new Date();
var mytime=myDate.toLocaleDateString().split('/').join('-');
Y = myDate.getFullYear(), // 年
M = myDate.getMonth() + 1, //月
D = myDate.getDate(), //日
H = myDate.getHours(), //时
M = myDate.getMinutes(), //分
S = myDate.getSeconds(), //秒
timer = Y+ '-' + getZero(M) + '-' + getZero(D)+' '+getZero(H)+':'+getZero(M)+':'+getZero(S); //输出形式以-进行连接
function getZero(num) { // 个位数前补0
if (parseInt(num) < 10) {
num = '0' + num;
}
return num;
return timer; //输出格式为2020-02-05 00:00:00
}
对该时间进行+1
//增加1天
function addDate(time) {
//加1天
var timestamp = Date.parse(new Date(time));
timestamp = timestamp /1000;
timestamp += 86400;//加1天
var newTime =new Date(timestamp * 1000).format('yyyy-MM-dd HH:mm:ss');
return newTime;
}
//日期格式
Date.prototype.format = function(format) {
var date = {
"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+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (var k in date) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1
? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
}
}
return format;
}
var newtime=add(timer);
console.log(newtime);