环境
3.7.5
当前时间
Date
new Date()
long
new Date().getTime();
当天0点时间
new Date(new Date().toLocaleDateString()).getTime()
昨天
endTime = new Date(new Date().toLocaleDateString()).getTime()-1
//0点时间
startTime = new Date(new Date().toLocaleDateString()).getTime()-24*60*60*1000
7天内时间
startTime = new Date(new Date().toLocaleDateString()).getTime()-7*24*60*60*1000
30天内时间
startTime = new Date(new Date().toLocaleDateString()).getTime()-30*24*60*60*1000
long 转yyyy-MM-dd hh:mm:ss
dateFormat(date: Date, sFormat: String = 'yyyy-MM-dd hh:mm:ss', timetype = "24-hour"): string {
let time = {
Year: 0,
TYear: '0',
Month: 0,
TMonth: '0',
Day: 0,
TDay: '0',
Hour: 0,
THour: '0',
hour: 0,
Thour: '0',
Minute: 0,
TMinute: '0',
Second: 0,
TSecond: '0',
Millisecond: 0
};
time.Year = date.getFullYear();
time.TYear = String(time.Year).substr(2);
time.Month = date.getMonth() + 1;
time.TMonth = time.Month < 10 ? "0" + time.Month : String(time.Month);
time.Day = date.getDate();
time.TDay = time.Day < 10 ? "0" + time.Day : String(time.Day);
time.Hour = date.getHours();
time.THour = time.Hour < 10 ? "0" + time.Hour : String(time.Hour);
if (timetype == '24-hour') {
time.hour = time.Hour
} else {
time.hour = time.Hour < 13 ? time.Hour : time.Hour - 12;
}
time.Thour = time.hour < 10 ? "0" + time.hour : String(time.hour);
time.Minute = date.getMinutes();
time.TMinute = time.Minute < 10 ? "0" + time.Minute : String(time.Minute);
time.Second = date.getSeconds();
time.TSecond = time.Second < 10 ? "0" + time.Second : String(time.Second);
time.Millisecond = date.getMilliseconds();
return sFormat.replace(/yyyy/ig, String(time.Year))
.replace(/yyy/ig, String(time.Year))
.replace(/yy/ig, time.TYear)
.replace(/y/ig, time.TYear)
.replace(/MM/g, time.TMonth)
.replace(/M/g, String(time.Month))
.replace(/dd/ig, time.TDay)
.replace(/d/ig, String(time.Day))
.replace(/HH/g, time.THour)
.replace(/H/g, String(time.Hour))
.replace(/hh/g, time.Thour)
.replace(/h/g, String(time.hour))
.replace(/mm/g, time.TMinute)
.replace(/m/g, String(time.Minute))
.replace(/ss/ig, time.TSecond)
.replace(/s/ig, String(time.Second))
.replace(/fff/ig, String(time.Millisecond))
}
yyyy-MM-dd hh:mm:ss转long
new Date("2020-05-19 00:00:00").getTime()