JavaScript 獲取當前系統時間(精確到天)


JavaScript 獲取當前系統時間(精確到天)

最近在使用 uni-app 開發項目,有個需求需要獲取當前系統時間,精確到天,同時是規范的時間格式。

獲取當前系統時間:

//輸出為:2020-09-07的格式
getDate: function() {
     const date = new Date();
     //2020
     let year = date.getFullYear();
     let month = date.getMonth() + 1;
     //09
     month = month > 9 ? month : '0' + month;
     let day = date.getDate();
     //07
     day = day > 9 ? day : '0' + day;
     console.log(`${year}-${month}-${day}`);
    //`${}`返回拼接的字符串
     return `${year}-${month}-${day}`;
 },

獲取月初時間:

getLastDate: function() {
    const date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    month = month > 9 ? month : '0' + month;
    return `${year}-${month}-${'01'}`;
},


免責聲明!

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



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