简体   繁体   English

如何在angular2中以24小时格式获取2次之间的时差

[英]How to get time difference between 2 times which are in 24 hours format in angular2

I want to calculate time difference between two times which are in 24hours format. 我想以24小时格式计算两次之间的时差。 Suppose start time is 23.00 PM and endtime is 2.00 AM. 假设开始时间为23.00 PM,结束时间为2.00 AM。 Then it should show 3 hours. 然后它应该显示3个小时。

Can anyone please help me how to do this. 谁能帮我怎么做。

To compare dates, the best way is to use the date in milliseconds with the function getTime() . 要比较日期,最好的方法是将getTime()函数与日期结合使用。

const diffInMilliseconds = endTime.getTime() - starTime.getTime() // 10800000
const diffInHours = diffInMilliseconds / 3600000 // 3 hours

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM