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'}`;
},