English 中文(简体)
我如何计算两个日期之间的差数?
原标题:How do I calculate how many seconds between two dates?

因此,我有两个日期<代码>。 缩略语

我怎么能发现它们之间有多少次?

最佳回答

I m 计及YYYY & ZZZZZZZ系指年、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格式出现,那么你可以 par。

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

并且,你可以把这一价值作为微秒的区别(如果我的两个例子都具有同样的含义)

问题回答

简略:

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);
var a = new Date("2010 jan 10"),
    b = new Date("2010 jan 9");

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

2. 便利的方式:

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)




相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...

热门标签