我有两个日期YYYY-MM-DD和ZZZZ-NN-EE
我怎么知道它们之间有多少秒呢?
我有两个日期YYYY-MM-DD和ZZZZ-NN-EE
我怎么知道它们之间有多少秒呢?
当前回答
创建两个Date对象并在两者上调用valueOf(),然后比较它们。
JavaScript日期对象参考
其他回答
. 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
简单的方法:
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));
创建两个Date对象并在两者上调用valueOf(),然后比较它们。
JavaScript日期对象参考
我用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);
一个便于将来参考的资源是MDN站点
或者,如果你的日期以javascript可以解析的格式出现
var dif = Date.parse(MM + " " + DD + ", " + YYYY) - Date.parse(NN + " " + EE + ", " + ZZZZ);
然后您可以使用该值作为毫秒差(dif在我的两个例子中具有相同的含义)
减:
var a = new Date(); 警报(“等待几秒钟,然后点击确定”); var b = new Date(); Var差= (b - a) / 1000; console.log("You waiting: " + difference + " seconds");