JS获取当前时间往后一天的时间

JS获取当前时间往后一天的时间_第1张图片

(图片来自W3school)

let myDate = new Date();
myDate.setDate(myDate.getDate() + 1);
let year = myDate.getFullYear();//getYear()方法也能获取但是部分浏览器不兼容,所以用getFullYear()方法
let month = myDate.getMonth() + 1;   //获取的数值是0-11,用来表示月份要+1
let day = myDate.getDay();
let hour = myDate.getHours();
let minute = myDate.getMinutes();
let second = myDate.getSeconds();
if (month < 10) {
    month = "0" + month;
}

if (day < 10) {
    day = "0" + day;
}

if (hour < 10) {
    hour = "0" + hour;
}

if (minute < 10) {
    minute = "0" + minute;
}

if (second < 10) {
    second = "0" + second;
}

let nowTime = `${year}-${month}-${day} ${hour}:${minute}:${second}`;

 

你可能感兴趣的:(JS获取当前时间往后一天的时间)