以HH:mm格式计算时差 [英] calculate time difference in HH:mm format
本文介绍了以HH:mm格式计算时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 HH:mm
格式的两个时间戳,我需要计算它们之间的差异,表示相同 HH中的时间间隔:mm
format。
I have two timestamps in HH:mm
format and I need to calculate difference between them representing the time interval in the same HH:mm
format.
JavaScript中是否有任何实用程序可以实现此目的?我尝试使用 Date
对象,但我找不到有用的东西......你能帮助我吗?
Is there any utility in JavaScript to achieve this? I tried using Date
object, but I cannot find something useful... Can you help me?
推荐答案
你可以直接减去两个日期,结果将是以毫秒为单位的差异。
You can just substract two Dates from one another, the result will be the difference in milliseconds.
// using static methods
var start = Date.now();
// the event you'd like to time goes here:
doSomethingForALongTime();
var end = Date.now();
var elapsed = end - start; // time in milliseconds
由于Date有一个构造函数接受毫秒作为参数,你可以重新通过这样做将其转换为日期
Since Date has a constructor that accepts milliseconds as an argument, you can re-convert this to a Date by just doing
var difference = new Date(elapsed);
//If you really want the hours/minutes,
//Date has functions for that too:
var diff_hours = difference.getHours();
var diff_mins = difference.getMinutes();
这篇关于以HH:mm格式计算时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文