如何计算两个日期之间的间隔秒数? [英] How do I calculate how many seconds between two dates?

查看:78
本文介绍了如何计算两个日期之间的间隔秒数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有两个日期 YYYY-MM-DD ZZZZ-NN-EE

我如何找出它们之间有多少秒?

How can I find out how many seconds there are between them?

推荐答案

我正在使用YYYY&ZZZZ表示整数值,表示年份MM&NN表示整数值,表示一年中的月份以及DD&EE为整数值,表示每月的某天.

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.

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可以解析的格式

Alternatively, if your dates come in a format javascript can parse

var dif = Date.parse(MM + " " + DD + ", " + YYYY) - Date.parse(NN + " " + EE + ", " + ZZZZ);

然后您可以将该值用作毫秒之间的差异(在我的两个示例中,dif的含义相同)

and then you can use that value as the difference in milliseconds (dif in both my examples has the same meaning)

这篇关于如何计算两个日期之间的间隔秒数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆