JS的时间差换算(String to 标准的时间格式)
1.字符串到标准时间格式:
字符串:
var time1="2018-05-11 00:00:00"
var time2="2018-05-12 00:00:00"
转到标准时间格式:
time1 = new Date(time1);
time2 = new Date(time2);
2.获取时间戳的差值
var interval=time2.getTime()-time1.getTime()
3.转换为自己所要求的时间格式
var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var dayC = interval / day;
var hourC = interval / hour - parseInt(dayC) * 24;
var minC = interval / minute - parseInt(dayC) * 24 * 60 - parseInt(hourC) * 60;
var result = ‘‘;
if (dayC == 0 && hourC == 0 && minC == 0) {
result = "刚刚";
}
else {
result = "" + parseInt(dayC) + "天" + parseInt(hourC) + "小时" + parseInt(minC) + "分钟";
}
result即为结果
原文地址:https://www.cnblogs.com/Archer-Fang/p/9032011.html