![](/img/trans.png)
[英]Calculate the difference between two dates of format (YYYY-MM-DD HH:MM) and display results in minutes
[英]Calculate the difference between two dates and give the result in hh:mm format
date1=2023-01-02T12:22:00
date2=2023-01-03T22:15:00
我怎么能找到时差?
你可以参考这里的答案: https://stackoverflow.com/a/12193371/9067107
这些步骤首先是通过new Date()
转换 UTC 时间,并通过date1 - date2
计算差异并获得不同的小时/分钟。
让我们假设你想在字符串中使用 HH:mm 格式
let date1 = new Date('2023-01-02T12:22:00')
let date2 = new Date('2023-01-03T22:15:00')
let diff = new Date(date2 - date1)
let diffInHoursMinutes = `${diff.getHours()}:${diff.getMinutes()}`
function secondsToHMS(secs) {
function z(n){return (n<10?'0':'') + n;}
var sign = secs < 0 ? '-' : '';
secs = Math.abs(secs);
return sign + z(secs/3600 |0) + ':' + z((secs%3600) / 60 |0);
}
var d1 = new Date('2023-01-03T22:15:00');
var d2 = new Date('2023-01-02T12:22:00');
//console.log( secondsToHMS((d1 - d2) / 1000)); // 33:53
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.