如何获得两个日期对象之间的小时差? [英] How to get the hours difference between two date objects?
本文介绍了如何获得两个日期对象之间的小时差?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个 Date 对象,我想以小时为单位计算差异.
I got two Date objects and I want to calculate the difference in hours.
如果小时差小于 18 小时,我想将日期对象推入数组.
If the difference in hours is less than 18 hours, I want to push the date object into an array.
Javascript/jQuery,并不重要;什么效果最好.
Javascript / jQuery, doesn't really matter; what works the best will do.
推荐答案
最简单的方法是直接从另一个日期对象中减去.
The simplest way would be to directly subtract the date objects from one another.
例如:
var hours = Math.abs(date1 - date2) / 36e5;
减法以毫秒为单位返回两个日期之间的差值.36e5
是60*60*1000
的科学记数法,除以毫秒差为小时.
The subtraction returns the difference between the two dates in milliseconds. 36e5
is the scientific notation for 60*60*1000
, dividing by which converts the milliseconds difference into hours.
这篇关于如何获得两个日期对象之间的小时差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文