如何获取两个日期对象之间的时差? [英] How to get the hours difference between two date objects?

查看:117
本文介绍了如何获取两个日期对象之间的时差?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Date对象,我想计算小时差。



如果小时差小于18小时,我想推送日期对象成一个数组。



Javascript / jQuery,并不重要;

解决方案

最简单的方法是直接从彼此减去日期对象。 >

例如:

  var hours = Math.abs(date1  -  date2) / 36e5; 

减法返回两个日期之间的差异(以毫秒为单位)。 36e5 60 * 60 * 1000 的科学符号,除以将毫秒差变换为小时。 p>

I got two Date objects and I want to calculate the difference in hours.

If the difference in hours is less than 18 hours, I want to push the date object into an array.

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.

For example:

var hours = Math.abs(date1 - date2) / 36e5;

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆