javascript

时间:2018-09-27 10:57:28

标签: javascript time difference

第一次尝试:

let time1 = new Date("September 24 2018 22:20");
let time2 = new Date("September 25 2018 02:00");

let diff = time2 - time1;
console.log(diff); // 13200000
let result = Math.abs(time1 - time2);
console.log(result); //13200

let hours = Math.floor(result / 3600) % 24;
console.log("Difference (Hours): " + hours); //3
let minutes = Math.floor(result / 60) % 60;
console.log("Difference (Minutes): " + minutes);//40

第二次尝试:

let options = { timeZone: 'UTC', timeZoneName: 'short'};
let time1 = new Date("September 24 2018 22:20");
let time2 = new Date("September 25 2018 02:00");

let diff = time2 - time1;
console.log(diff); //13200000
let result = new Date(diff);
console.log(result.toLocaleTimeString("de-DE", options)); //03:40:00 UTC

所以,这两个前任。工作正常,但前提是两个日期之间的最大差值为23:59。 但是我想要显示超过23:59的小时和分钟,例如ex,date1 =两周前的星期五22:43,date2 =今天/ 2天,等等,所以我得到的结果是127:44或54: 52或48:33。

我知道我做错了什么,但是呢?

0 个答案:

没有答案