Reputation: 13537
So I have two dates YYYY-MM-DD
and ZZZZ-NN-EE
How can I find out how many seconds there are between them?
Upvotes: 257
Views: 255615
Reputation: 33
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)
Upvotes: 0
Reputation: 46423
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");
Upvotes: 117
Reputation: 895
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));
Upvotes: 6
Reputation: 12403
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);
A handy source for future reference is the MDN site
Alternatively, if your dates come in a format javascript can parse
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)
Upvotes: 333
Reputation:
You can do it simply.
var secondBetweenTwoDate = Math.abs((new Date().getTime() - oldDate.getTime()) / 1000);
Upvotes: 25
Reputation: 211
In 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.
Upvotes: -3
Reputation: 315
.Net provides the TimeSpan class to do the math for you.
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
Upvotes: -12
Reputation: 25696
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).
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
Upvotes: 16
Reputation: 163228
create two Date
objects and call valueOf()
on both, then compare them.
JavaScript Date Object Reference
Upvotes: 6
Reputation: 31300
var a = new Date("2010 jan 10"),
b = new Date("2010 jan 9");
alert(
a + "\n" +
b + "\n" +
"Difference: " + ((+a - +b) / 1000)
);
Upvotes: 6