先将日期字符串转换为时间戳
1、使用: new Date().getTime();
new Date("2018-09-09 12:30:22").getTime();
// 1536467422000
- 使用: Date.parse();
Date.parse("2018/09/09 12:30:22");
// 1536467422000
直接上代码
getDateDiff(startTime, endTime) {
//将日期字符串转换为时间戳
var sTime = new Date(startTime).getTime(); //开始时间
var eTime = new Date(endTime).getTime(); //结束时间
//作为除数的数字
var divNumSecond = 1000;
var divNumMinute = 1000 * 60;
var divNumHour = 1000 * 3600;
var divNumDay = 1000 * 3600 * 24;
const day = parseInt((eTime - sTime) / parseInt(divNumDay))
const hour = parseInt(((eTime - sTime) % parseInt(divNumDay)) / parseInt(divNumHour))
const minute = parseInt((parseInt(((eTime - sTime) % parseInt(divNumDay)) % parseInt(divNumHour))) / parseInt(divNumMinute))
const second = ((parseInt(((eTime - sTime) % parseInt(divNumDay)) % parseInt(divNumHour))) % parseInt(divNumMinute)) / parseInt(divNumSecond)
const str = day + '天' + hour + '小时' + minute + '分' + second + '秒'
return str
}
例如:
this. getDateDiff("2021-05-23 12:33:22", "2021-05-26 15:22:12")
网友评论