Javascript - 查找两个MM-dd-yyyy之间的秒数HH:mm:ss格式的日期? [英] Javascript - Find seconds between two MM-dd-yyyy HH:mm:ss formatted dates?
问题描述
我正在尝试使用两个MM-dd-yyyy HH:mm:ss格式的日期来查找它们之间的秒数。如何做这个?
I am trying to use two MM-dd-yyyy HH:mm:ss formatted dates to find the time in seconds between them. How do I go about doing this?
推荐答案
使用 Date.parse(...) code>来解析日期来创建一个
Date
对象。
在创建的对象上使用 .valueOf()
,您将获得这个时间(1970年1月1日以来的毫秒)日期。
With .valueOf()
on the created object you get the epoch (milliseconds since January 1st 1970) of this date.
同时使用这两个日期并对数字进行减法。不同之处在于日期之间的毫秒数。其余的应该是清楚的:)
Do this with both dates and substract the numbers. The difference is the time in milliseconds between the dates. The rest should be clear :)
编辑:
,这里是对 Date
对象的更有效的引用:
http://www.ecma-international.org/ecma-262/6.0/index.html#sec-date-constructor
Following discussion in comments, here is the more valid reference to Date
object:
http://www.ecma-international.org/ecma-262/6.0/index.html#sec-date-constructor
这篇关于Javascript - 查找两个MM-dd-yyyy之间的秒数HH:mm:ss格式的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!