1. 程式人生 > >獲取昨天日期

獲取昨天日期

有個專案要用到昨天的日期,精確到天。但是網上的沒有找到相應的程式碼,so自己寫了個簡單的。

getYesterday() {
let oneday = 24*60*60*1000;
let timestamp =  Date.parse(new Date())-oneday;
let y = new Date(timestamp).getFullYear()+ '-';
let m = (new Date(timestamp).getMonth()+1 < 10 ? '0'+(new Date(timestamp).getMonth()+1) : new Date(timestamp).getMonth()+1) + '-';
let d = new Date(timestamp).getDate()<10?'0'+new Date(timestamp).getDate() : new Date(timestamp).getDate();
return y+m+d
}