JavaScript 中日期之间的差异
如何找出两个日期之间的差异?
How to find the difference between two dates?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何找出两个日期之间的差异?
How to find the difference between two dates?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
通过使用 Date 对象及其毫秒值,可以计算差异:
您可以得到秒数(作为整数/整数),方法是将毫秒除以 1000 将其转换为秒,然后将结果转换为整数(这会删除代表毫秒的小数部分):
然后您可以获得整数分钟< /code> 将秒除以 60 并将其转换为整数,然后将小时除以 60 将分钟转换为整数,然后以同样的方式更长的时间单位。由此,可以创建一个函数来获取下一个单位的值中的时间单位的最大总量和余下的下单位:
如果您想知道上面为第二个 Date 对象 是,请参阅下面的名称:
正如此解决方案的注释中所述,您不一定需要提供所有这些值,除非它们对于您希望表示的日期是必需的。
By using the Date object and its milliseconds value, differences can be calculated:
You can get the number of seconds (as a integer/whole number) by dividing the milliseconds by 1000 to convert it to seconds then converting the result to an integer (this removes the fractional part representing the milliseconds):
You could then get whole
minutes
by dividingseconds
by 60 and converting it to an integer, thenhours
by dividingminutes
by 60 and converting it to an integer, then longer time units in the same way. From this, a function to get the maximum whole amount of a time unit in the value of a lower unit and the remainder lower unit can be created:If you're wondering what the input parameters provided above for the second Date object are, see their names below:
As noted in the comments of this solution, you don't necessarily need to provide all these values unless they're necessary for the date you wish to represent.
我发现了这个,它对我来说效果很好:
计算两个已知日期之间的差异
不幸的是,计算两个已知日期之间的日期间隔(例如天、周或月)并不那么容易,因为您可以不仅仅是将 Date 对象添加在一起。为了在任何类型的计算中使用 Date 对象,我们必须首先检索 Date 的内部毫秒值,该值存储为一个大整数。执行此操作的函数是 Date.getTime()。转换两个日期后,用前一个日期减去后一个日期将返回以毫秒为单位的差值。然后可以通过将该数字除以相应的毫秒数来确定所需的间隔。例如,要获取给定毫秒数的天数,我们将除以一天中的毫秒数 86,400,000(1000 x 60 秒 x 60 分钟 x 24 小时):
舍入是可选的,具体取决于是否你是否想要部分日子。
参考
I have found this and it works fine for me:
Calculating the Difference between Two Known Dates
Unfortunately, calculating a date interval such as days, weeks, or months between two known dates is not as easy because you can't just add Date objects together. In order to use a Date object in any sort of calculation, we must first retrieve the Date's internal millisecond value, which is stored as a large integer. The function to do that is Date.getTime(). Once both Dates have been converted, subtracting the later one from the earlier one returns the difference in milliseconds. The desired interval can then be determined by dividing that number by the corresponding number of milliseconds. For instance, to obtain the number of days for a given number of milliseconds, we would divide by 86,400,000, the number of milliseconds in a day (1000 x 60 seconds x 60 minutes x 24 hours):
The rounding is optional, depending on whether you want partial days or not.
Reference
如果您正在寻找以年、月和日组合表示的差异,我建议使用以下函数:
这个解决方案将以我们自然会做的方式处理闰年(2 月 29 日)和月份长度差异(我认为)。
例如,2015年2月28日和2015年3月28日之间的间隔将被视为正好一个月,而不是28天。如果这两个日子都是 2016 年,那么差异仍然是 1 个月,而不是 29 天。
月份和日期完全相同但年份不同的日期始终会存在精确的年数差异。因此 2015-03-01 和 2016-03-01 之间的差异将恰好是 1 年,而不是 1 年零 1 天(因为将 365 天算作 1 年)。
If you are looking for a difference expressed as a combination of years, months, and days, I would suggest this function:
This solution will treat leap years (29 February) and month length differences in a way we would naturally do (I think).
So for example, the interval between 28 February 2015 and 28 March 2015 will be considered exactly one month, not 28 days. If both those days are in 2016, the difference will still be exactly one month, not 29 days.
Dates with exactly the same month and day, but different year, will always have a difference of an exact number of years. So the difference between 2015-03-01 and 2016-03-01 will be exactly 1 year, not 1 year and 1 day (because of counting 365 days as 1 year).
这个答案基于另一个答案(链接在最后),是关于两个日期之间的差异。
您可以看到它是如何工作的,因为它很简单,还包括将差异分为
时间单位(我制作的函数)并转换为 UTC 以解决时区问题。
我上面的代码是如何工作的
日期/时间差异(以毫秒为单位)可以使用 Date 对象:
然后计算出该差异的秒数,将其除以 1000 进行转换
毫秒转为秒,然后将结果改为整数(整数)即可去除
毫秒(小数的小数部分):
var秒= parseInt(diff/1000)
。另外,我可以使用相同的过程获得更长的时间单位,例如:
-(整数)分钟,将秒除以 60 并将结果更改为整数,
- 小时,将分钟除以60并将结果更改为整数。
我创建了一个函数来执行将差异分成
的过程
整个时间单位,名为
split_to_whole_units
,通过此演示:此答案基于另一个。
This answer, based on another one (link at end), is about the difference between two dates.
You can see how it works because it's simple, also it includes splitting the difference into
units of time (a function that I made) and converting to UTC to stop time zone problems.
How my code above works
A date/time difference, as milliseconds, can be calculated using the Date object:
Then to work out the number of seconds in that difference, divide it by 1000 to convert
milliseconds to seconds, then change the result to an integer (whole number) to remove
the milliseconds (fraction part of that decimal):
var seconds = parseInt(diff/1000)
.Also, I could get longer units of time using the same process, for example:
- (whole) minutes, dividing seconds by 60 and changing the result to an integer,
- hours, dividing minutes by 60 and changing the result to an integer.
I created a function for doing that process of splitting the difference into
whole units of time, named
split_to_whole_units
, with this demo:This answer is based on this other one.
您还可以使用它的
结果 { result: '30d', Days: 30, Mins: 43200, Hours: 720 }
You can also use it
results in { result: '30d', Days: 30, Mins: 43200, Hours: 720 }