如何在JavaScript中计算日期差异?
我想计算以天、小时、分钟、秒、毫秒、纳秒为单位的日期差异。我该怎么做呢?
I want to calculate date difference in days, hours, minutes, seconds, milliseconds, nanoseconds. How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(24)
假设您有两个
Date
对象,您只需减去它们即可得到以毫秒为单位的差异:从那里,您可以使用简单的算术来导出其他值。
Assuming you have two
Date
objects, you can just subtract them to get the difference in milliseconds:From there, you can use simple arithmetic to derive the other values.
代码示例取自此处。
Code sample taken from here.
另一个解决方案是将差异转换为新的 Date 对象并获取该日期的年(与 1970 年不同)、月、日等。
所以差异就像“3 年 6 个月又 4 天”。如果您想以人类可读的风格来区分,这可以帮助您。
Another solution is convert difference to a new Date object and get that date's year(diff from 1970), month, day etc.
So difference is like "3 years and 6 months and 4 days". If you want to take difference in a human readable style, that can help you.
像“天差地别”这样的表达从来都不像看起来那么简单。如果有以下日期:
时间差为 2 分钟,“天数差”应该是 1 还是 0?任何以月、年或其他形式表达差异的情况都会出现类似的问题,因为年、月和日的长度和时间不同(例如,夏令时开始的那天比平常短 1 小时,比平常短 2 小时)。就结束了)。
这是一个忽略时间的天差函数,即对于上述日期,它返回 1。
这可以更简洁:
Expressions like "difference in days" are never as simple as they seem. If you have the following dates:
the difference in time is 2 minutes, should the "difference in days" be 1 or 0? Similar issues arise for any expression of the difference in months, years or whatever since years, months and days are of different lengths and different times (e.g. the day that daylight saving starts is 1 hour shorter than usual and two hours shorter than the day that it ends).
Here is a function for a difference in days that ignores the time, i.e. for the above dates it returns 1.
This can be more concise:
使用 Moment.js 进行所有与 JavaScript 相关的日期时间计算
您的问题的答案是:
完整的详细信息可以找到 此处
use Moment.js for all your JavaScript related date-time calculation
Answer to your question is:
Complete details can be found here
添加到 @paresh mayani 的答案,像 Facebook 一样工作 - 显示以秒/分钟/小时/周/月/年为单位过去了多少时间
adding to @paresh mayani 's answer, to work like Facebook - showing how much time has passed in sec/min/hours/weeks/months/years
使用 momentjs 很简单:
With momentjs it's simple:
我认为这应该可以做到。
I think this should do it.
基于javascript运行时原型实现,您可以使用简单的算术来减去日期,如下所示,
差值返回结果为毫秒,然后您必须通过除法进行转换:
based on javascript runtime prototype implementation you can use simple arithmetic to subtract dates as in bellow
the difference return answer as milliseconds, then you have to convert it by division:
此代码将返回两个日期之间的天数差异:
This code will return the difference between two dates in days:
抱歉,平毫秒计算不可靠
感谢您的所有回复,但我尝试过的一些功能都失败了
1. 接近今天的日期
2. 1970 年的日期或
3. 闰年的日期。
最适合我的方法,涵盖所有场景,例如闰年、1970 年临近日期、2 月 29 日等。
Sorry but flat millisecond calculation is not reliable
Thanks for all the responses, but few of the functions I tried are failing either on
1. A date near today's date
2. A date in 1970 or
3. A date in a leap year.
Approach that best worked for me and covers all scenario e.g. leap year, near date in 1970, feb 29 etc.
如果您使用 moment.js,那么查找日期差异非常简单。
If you are using moment.js then it is pretty simple to find date difference.
这就是如何在没有框架的情况下实现日期之间的差异。
This is how you can implement difference between dates without a framework.
如果你只需要显示剩余时间,这应该可以正常工作,因为 JavaScript 使用帧作为时间,你将得到你的结束时间 - 时间 RN 之后我们可以将它除以 1000,因为显然 1000 帧 = 1 秒,之后你可以使用基本的时间数学,但是这个代码仍然有一个问题,因为计算是静态的,它无法补偿一年中不同日期的总数(360/365/366),一堆计算后的 IF 的作用是,如果时间低于 0,则将其设为空,希望这会有所帮助,即使这不完全是您所要求的:)
this should work just fine if you just need to show what time left, since JavaScript uses frames for its time you'll have get your End Time - The Time RN after that we can divide it by 1000 since apparently 1000 frames = 1 seconds, after that you can use the basic math of time, but there's still a problem to this code, since the calculation is static, it can't compensate for the different day total in a year (360/365/366), the bunch of IF after the calculation is to make it null if the time is lower than 0, hope this helps even though it's not exactly what you're asking :)
我做了一个下面的函数来获取 now 和
"2021-02-26T21:50:42.123"
之间的区别。差异以毫秒形式返回答案,因此我使用以下公式进行转换:
(1000 * 3600 * 24)
。I made a below function to get the difference between now and
"2021-02-26T21:50:42.123"
.The difference return answer as milliseconds, so I convert it by using this formula:
(1000 * 3600 * 24)
.好的,有很多方法可以做到这一点。
是的,你可以使用普通的旧 JS。尝试一下:
让我们使用 Date.prototype.setMinutes 来模拟段落并确保我们在范围内。
或者,您可以使用一些库,例如 js-joda,在这里你可以轻松地做这样的事情(直接来自文档):
还有更多的 ofc 库,但是 js-joda 还有一个额外的好处,那就是它也可以在 Java 中使用,它已经在 Java 中进行了广泛的测试。所有这些测试都已迁移到 js-joda,它也是不可变的。
Ok, there are a bunch of ways you can do that.
Yes, you can use plain old JS. Just try:
Let's emulate passage using Date.prototype.setMinutes and make sure we are in range.
Alternatively you could use some library like js-joda, where you can easily do things like this (directly from docs):
There are plenty more libraries ofc, but js-joda has an added bonus of being available also in Java, where it has been extensively tested. All those tests have been migrated to js-joda, it's also immutable.
可能有用:
或者
其中 24 * 60 * 60 * 1000 是(天 * 分钟 * 秒 * 毫秒)= 86400000 毫秒一天
谢谢
Can be useful :
or
where 24 * 60 * 60 * 1000 is (day * minutes * seconds * milliseconds) = 86400000 milliseconds in one day
Thank you