![](/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.