JS獲取當前時間往后一天的時間


(圖片來自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}`;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM