简体   繁体   English

计算两个日期之间的差异,并以 hh:mm 格式给出结果

[英]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

How could I found the time difference?我怎么能找到时差?

You can refer to the answer here: https://stackoverflow.com/a/12193371/9067107你可以参考这里的答案: https://stackoverflow.com/a/12193371/9067107

The steps are firstly converting the UTC time via new Date() and calculate the different by date1 - date2 and get the Hours / Minutes different.这些步骤首先是通过new Date()转换 UTC 时间,并通过date1 - date2计算差异并获得不同的小时/分钟。

Lets assume you want to have HH:mm format in string让我们假设你想在字符串中使用 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.

相关问题 计算两个日期格式(YYYY-MM-DD HH:MM)之间的差异,并以分钟为单位显示结果 - Calculate the difference between two dates of format (YYYY-MM-DD HH:MM) and display results in minutes 格式中的两次(不是日期)之间的差异(hh:mm) - Difference between two times (not dates) in format (hh:mm) 如何使用 yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffff 日期时间格式计算两个日期之间的差异 - How to Calculate difference between two dates with yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffff date-time format Moment.js以dd:hh:mm:ss格式获取两个日期之间的差异? - Moment.js Get difference between two dates in the dd:hh:mm:ss format? Jquery - 在两个日期之间以HH:MM格式获取时间 - Jquery - Get the time in HH:MM format between two dates 在 HH:MM:SS javascript 中计算两个日期之间的时差 - calculate time difference between two date in HH:MM:SS javascript 以hh:mm:ss格式获取两次之间的时间差 - get the time difference between two times in format hh:mm:ss 两次格式的差异(hh:mm:ss) - Difference between two times in format (hh: mm: ss) 以 HH:mm 格式计算时差 - calculate time difference in HH:mm format 如何在本机反应中以 {dd-mm-yyyy} 格式计算两个日期之间的差异 - How to calculate difference between two dates in {dd-mm-yyyy} format in react native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM