简体   繁体   English

如何计算两个日期之间的秒数?

[英]How do I calculate how many seconds between two dates?

So I have two dates YYYY-MM-DD and ZZZZ-NN-EE所以我有两个日期YYYY-MM-DDZZZZ-NN-EE

How can I find out how many seconds there are between them?我怎样才能知道它们之间有多少秒?

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)然后您可以将该值用作以毫秒为单位的差异(我的两个示例中的差异具有相同的含义)

Just subtract:只需减去:

 var a = new Date(); alert("Wait a few seconds, then click OK"); var b = new Date(); var difference = (b - a) / 1000; console.log("You waited: " + difference + " seconds");

你可以简单地做到这一点。

var secondBetweenTwoDate = Math.abs((new Date().getTime() - oldDate.getTime()) / 1000);

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.如果您的一个或两个日期都在未来,那么如果您想要秒精度,恐怕您就是 SOL。 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). 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

create two Date objects and call valueOf() on both, then compare them.创建两个Date对象并在两者上调用valueOf() ,然后比较它们。

JavaScript Date Object Reference JavaScript 日期对象参考

var a = new Date("2010 jan 10"),
    b = new Date("2010 jan 9");

alert(
    a + "\n" + 
    b + "\n" +
    "Difference: " + ((+a - +b) / 1000)
);

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));
function parseDate(str) {
  const [dateStr, timeStr] = str.split(' ');
  const [m,d,y] = dateStr.split('/');
  const [h,min] = timeStr.split(':');
  const date = new Date(y,m,d,h,min,0, 0);
  
  return date;
}

const date1 = parseDate('28/6/2022 22:55');
const date2 = parseDate('28/6/2022 22:58');
const diffInSeconds = (date2 - date1) / 1000;

console.log(diffInSeconds)

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。

.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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM