Javascript 两个日期之间有多少秒?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2024198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How Many Seconds Between Two Dates?
提问by steven
So I have two dates YYYY-MM-DDand ZZZZ-NN-EE
所以我有两个日期YYYY-MM-DD和ZZZZ-NN-EE
How can I find out how many seconds there are between them?
我怎样才能找出它们之间有多少秒?
回答by Martin
I'm taking YYYY & ZZZZ to mean integer values which mean the year, MM & NN to mean integer values meaning the month of the year and DD & EE as integer values meaning the day of the month.
我用 YYYY & ZZZZ 表示整数值,表示年份,MM & NN 表示整数值,表示一年中的月份,而 DD & EE 表示整数值,表示月份中的某一天。
var t1 = new Date(YYYY, MM, DD, 0, 0, 0, 0);
var t2 = new Date(ZZZZ, NN, EE, 0, 0, 0, 0);
var dif = t1.getTime() - t2.getTime();
var Seconds_from_T1_to_T2 = dif / 1000;
var Seconds_Between_Dates = Math.abs(Seconds_from_T1_to_T2);
A handy source for future reference is the MDN site
一个方便的供将来参考的来源是MDN 站点
Alternatively, if your dates come in a format javascript can parse
或者,如果您的日期采用 javascript 可以解析的格式
var dif = Date.parse(MM + " " + DD + ", " + YYYY) - Date.parse(NN + " " + EE + ", " + ZZZZ);
and then you can use that value as the difference in milliseconds (dif in both my examples has the same meaning)
然后您可以使用该值作为以毫秒为单位的差异(我的两个示例中的差异具有相同的含义)
回答by Seth
Just subtract:
只需减去:
var a = new Date();
alert("Wait a few seconds, then click OK");
var b = new Date();
var difference = (b - a) / 1000;
alert("You waited: " + difference + " seconds");
回答by rmeador
If one or both of your dates are in the future, then I'm afraid you're SOL if you want to-the-second accuracy. UTC time has leap seconds that aren't known until about 6 months before they happen, so any dates further out than that can be inaccurate by some number of seconds (and in practice, since people don't update their machines that often, you may find that any time in the future is off by some number of seconds).
如果您的一个或两个日期都在未来,那么如果您想要秒的准确性,恐怕您就是 SOL。UTC 时间有闰秒,直到它们发生前大约 6 个月才知道,所以任何比这更远的日期都可能不准确几秒(实际上,因为人们不经常更新他们的机器,你可能会发现将来的任何时间都关闭了一些秒数)。
This gives a good explanation of the theory of designing date/time libraries and why this is so: http://www.boost.org/doc/libs/1_41_0/doc/html/date_time/details.html#date_time.tradeoffs
这很好地解释了设计日期/时间库的理论以及为什么会这样:http: //www.boost.org/doc/libs/1_41_0/doc/html/date_time/details.html#date_time.tradeoffs
回答by rmeador
You can do it simply.
你可以简单地做到这一点。
var secondBetweenTwoDate = Math.abs((new Date().getTime() - oldDate.getTime()) / 1000);
回答by Jacob Relkin
create two Dateobjects and call valueOf()on both, then compare them.
创建两个Date对象并调用valueOf()它们,然后比较它们。
回答by Justin Johnson
var a = new Date("2010 jan 10"),
b = new Date("2010 jan 9");
alert(
a + "\n" +
b + "\n" +
"Difference: " + ((+a - +b) / 1000)
);
回答by Fernando Pie
Easy Way:
简单的方法:
function diff_hours(dt2, dt1)
{
var diff =(dt2.getTime() - dt1.getTime()) / 1000;
diff /= (60 * 60);
return Math.abs(Math.round(diff));
}
function diff_minutes(dt2, dt1)
{
var diff =(dt2.getTime() - dt1.getTime()) / 1000;
diff /= (60);
return Math.abs(Math.round(diff));
}
function diff_seconds(dt2, dt1)
{
var diff =(dt2.getTime() - dt1.getTime()) / 1000;
return Math.abs(Math.round(diff));
}
function diff_miliseconds(dt2, dt1)
{
var diff =(dt2.getTime() - dt1.getTime());
return Math.abs(Math.round(diff));
}
dt1 = new Date(2014,10,2);
dt2 = new Date(2014,10,3);
console.log(diff_hours(dt1, dt2));
dt1 = new Date("October 13, 2014 08:11:00");
dt2 = new Date("October 14, 2014 11:13:00");
console.log(diff_hours(dt1, dt2));
console.log(diff_minutes(dt1, dt2));
console.log(diff_seconds(dt1, dt2));
console.log(diff_miliseconds(dt1, dt2));
回答by Alex G
In bash:
在 bash 中:
bc <<< "$(date --date='1 week ago' +%s) - \
$(date --date='Sun, 29 Feb 2004 16:21:42 -0800' +%s)"
It does require having bc and gnu date installed.
它确实需要安装 bc 和 gnu date。
回答by Jeff.Crossett
.Net provides the TimeSpan class to do the math for you.
.Net 提供了 TimeSpan 类来为您做数学运算。
var time1 = new Date(YYYY, MM, DD, 0, 0, 0, 0)
var time2 = new Date(ZZZZ, NN, EE, 0, 0, 0, 0)
Dim ts As TimeSpan = time2.Subtract(time1)
ts.TotalSeconds

