If I have two dates, how can I use JavaScript to get the difference between the two dates in minutes?
-
are they date objects or just strings? please give an example situatin – amosrivera Oct 10 '11 at 07:42
13 Answers
You may checkout this code:
var today = new Date();
var Christmas = new Date(today.getFullYear() + "-12-25");
var diffMs = (Christmas - today); // milliseconds between now & Christmas
var diffDays = Math.floor(diffMs / 86400000); // days
var diffHrs = Math.floor((diffMs % 86400000) / 3600000); // hours
var diffMins = Math.round(((diffMs % 86400000) % 3600000) / 60000); // minutes
console.log(diffDays + " days, " + diffHrs + " hours, " + diffMins + " minutes until Christmas =)");
or var diffMins = Math.floor((...
to discard seconds if you don't want to round minutes.

- 331,213
- 40
- 305
- 339

- 1,023,142
- 271
- 3,287
- 2,928
-
10@philk, that's why I value StackOverflow so much. In many message boards, the answer is just a hyperlink. We wouldn't have this great answer if that was the case here. – Andrew Neely Feb 25 '15 at 19:11
-
5I found an error when testing this answer: DiffHrs may be wrong If you set the minutes in the Date object. For example if Christmas is "12-25-2015 03:55" and today is "12-25-2015 02:00" then the hourDiff is two hours. Should be one hour. – HoffZ Jun 17 '15 at 10:36
-
-
@DomVinyard It's subtracting the number of days and hours. E.g. 1 days, 2 hours, 5 min. If you omit `% 86400000) % 3600000)` it retuns the total number of minutes between the dates, 1565 min. – Justin Aug 18 '15 at 19:34
-
51`Math.round(((diffMs % 86400000) % 3600000) / 60000);` is not working when the difference is greater than 60 minutes – angularrocks.com Sep 03 '15 at 12:36
-
9Reiterating that this does NOT work properly, at least for minutes. For example, it took a difference of 62 minutes and due to the rounding, thought it was a difference of 2 minutes. – Jordan Jul 05 '16 at 22:38
-
1
-
13it doesn't answer the question correctly, instead of calculating the difference in ONLY minutes, it difference is calculated as a composite of days, hours and minutes...thus if the difference is 1 day 1 hour 1 minute, it will show 1 day 1 hour 1 minute, instead of 1501 minutes – Akhil Gupta Jan 11 '17 at 10:51
-
dividing by big numbers like 86400000 can screw you up if you're using a javascript engine with single precision like the old voice browser I'm dealing with at the moment `(date2 - date1) * 1/60` – gordy Jun 28 '17 at 02:00
-
-
1Old answer, but you should never use `new Date(string)` with a format other than an ISO-8601 format string. See [Why does Date.parse give incorrect results?](https://stackoverflow.com/q/2587345/215552), or just use numbers (e.g., `new Date(2011, 9, 9, 12, 0, 0)`; just remember months are 0 based). – Heretic Monkey Aug 15 '19 at 14:28
-
Subtracting two Date
objects gives you the difference in milliseconds, e.g.:
var diff = Math.abs(new Date('2011/10/09 12:00') - new Date('2011/10/09 00:00'));
Math.abs
is used to be able to use the absolute difference (so new Date('2011/10/09 00:00') - new Date('2011/10/09 12:00')
gives the same result).
Dividing the result by 1000 gives you the number of seconds. Dividing that by 60 gives you the number of minutes. To round to whole minutes, use Math.floor
or Math.ceil
:
var minutes = Math.floor((diff/1000)/60);
In this example the result will be 720.
[edit 2022] Added a more complete demo snippet, using the aforementioned knowledge.
untilXMas();
function difference2Parts(milliseconds) {
const secs = Math.floor(Math.abs(milliseconds) / 1000);
const mins = Math.floor(secs / 60);
const hours = Math.floor(mins / 60);
const days = Math.floor(hours / 24);
const millisecs = Math.floor(Math.abs(milliseconds)) % 1000;
const multiple = (term, n) => n !== 1 ? `${n} ${term}s` : `1 ${term}`;
return {
days: days,
hours: hours % 24,
hoursTotal: hours,
minutesTotal: mins,
minutes: mins % 60,
seconds: secs % 60,
secondsTotal: secs,
milliSeconds: millisecs,
get diffStr() {
return `${multiple(`day`, this.days)}, ${
multiple(`hour`, this.hours)}, ${
multiple(`minute`, this.minutes)} and ${
multiple(`second`, this.seconds)}`;
},
get diffStrMs() {
return `${this.diffStr.replace(` and`, `, `)} and ${
multiple(`millisecond`, this.milliSeconds)}`;
},
};
}
function untilXMas() {
const nextChristmas = new Date(Date.UTC(new Date().getFullYear(), 11, 25));
const report = document.querySelector(`#nextXMas`);
const diff = () => {
const diffs = difference2Parts(nextChristmas - new Date());
report.innerHTML = `Awaiting next XMas (${
diffs.diffStrMs.replace(/(\d+)/g, a => `<b>${a}</b>`)})<br>
<br>In other words, until next XMas lasts…<br>
In minutes: <b>${diffs.minutesTotal}</b><br>In hours: <b>${
diffs.hoursTotal}</b><br>In seconds: <b>${diffs.secondsTotal}</b>`;
setTimeout(diff, 200);
};
return diff();
}
body {
font: 14px/17px normal verdana, arial;
margin: 1rem;
}
<div id="nextXMas"></div>

- 119,216
- 31
- 141
- 177
-
8I think this is the better answer, I noticed the accepted answer fails to convert the difference to minute when the difference is more than 60 mins – sani Aug 17 '16 at 11:11
-
This is also wrong. 12:00 can't be the same as 00:00, as these are 12 hours difference... – Davatar Jan 04 '18 at 08:23
-
1@Davatar who says they are the same? he just states that 12 hours - 0 hours is the same as 0 hours - 12 hours. – Mark Baijens Feb 21 '18 at 11:38
-
1Old answer, but you should never use `new Date(string)` with a format other than an ISO-8601 format string. See [Why does Date.parse give incorrect results?](https://stackoverflow.com/q/2587345/215552), or just use numbers (e.g., `new Date(2011, 9, 9, 12, 0, 0)`; just remember months are 0 based). – Heretic Monkey Aug 15 '19 at 14:27
var startTime = new Date('2012/10/09 12:00');
var endTime = new Date('2013/10/09 12:00');
var difference = endTime.getTime() - startTime.getTime(); // This will give difference in milliseconds
var resultInMinutes = Math.round(difference / 60000);

- 1,761
- 3
- 18
- 29
-
2This is nice and simple and worked for me. I did remove the .getTime() from the dates though as it seemed more logical to me seen as the idea is to compare the two dates. Thanks. – Yos Dec 31 '13 at 10:11
-
1Old answer, but you should never use `new Date(string)` with a format other than an ISO-8601 format string. See [Why does Date.parse give incorrect results?](https://stackoverflow.com/q/2587345/215552), or just use numbers (e.g., `new Date(2011, 9, 9, 12, 0, 0)`; just remember months are 0 based). – Heretic Monkey Aug 15 '19 at 14:27
A simple function to perform this calculation:
function getMinutesBetweenDates(startDate, endDate) {
const diff = endDate.getTime() - startDate.getTime();
return (diff / 60000);
}

- 7,394
- 4
- 40
- 62

- 11,841
- 9
- 52
- 69
-
1Thanks for the simple encapsulation in a function. Here I would however use "let" instead of "var". – Robin Bastiaan Nov 22 '21 at 22:32
-
2
That's should show the difference between the two dates in minutes. Try it in your browser:
const currDate = new Date('Tue Feb 13 2018 13:04:58 GMT+0200 (EET)')
const oldDate = new Date('Tue Feb 13 2018 12:00:58 GMT+0200 (EET)')
(currDate - oldDate) / 60000 // 64

- 3,309
- 29
- 27
-
9TypeScript will give you the error "The left -hand and right hand side of an arithmetic operation must be of type 'any', 'number' or an enum type". To avoid the error do `(currDate.getTime() - oldDate.getTime()) / 60000` – Homer Oct 17 '19 at 21:08
This problem is solved easily with moment.js, like this example:
var difference = mostDate.diff(minorDate, "minutes");
The second parameter can be changed for another parameters, see the moment.js documentation.
e.g.: "days", "hours", "minutes", etc.
The CDN for moment.js is available here:
https://cdnjs.com/libraries/moment.js
Thanks.
EDIT:
mostDate and minorDate should be a moment type.
EDIT 2:
For those who are reading my answer in 2020+, momentjs is now a legacy project.
If you are still looking for a well-known library to do this job, I would recommend date-fns.
// How many minutes are between 2 July 2014 12:07:59 and 2 July 2014 12:20:00?
var result = differenceInMinutes(
new Date(2014, 6, 2, 12, 20, 0),
new Date(2014, 6, 2, 12, 7, 59)
)
//=> 12

- 933
- 9
- 22
You can do as follows:
- Get
difference of dates
(Difference will be in milliseconds) - Convert
milliseconds
intominutes
i-ems/1000/60
The Code:
let dateOne = new Date("2020-07-10");
let dateTwo = new Date("2020-07-11");
let msDifference = dateTwo - dateOne;
let minutes = Math.floor(msDifference/1000/60);
console.log("Minutes between two dates =",minutes);

- 5,227
- 4
- 13
- 29

- 219
- 2
- 3
For those that like to work with small numbers
const today = new Date();
const endDate = new Date(startDate.setDate(startDate.getDate() + 7));
const days = parseInt((endDate - today) / (1000 * 60 * 60 * 24));
const hours = parseInt(Math.abs(endDate - today) / (1000 * 60 * 60) % 24);
const minutes = parseInt(Math.abs(endDate.getTime() - today.getTime()) / (1000 * 60) % 60);
const seconds = parseInt(Math.abs(endDate.getTime() - today.getTime()) / (1000) % 60);

- 12,606
- 2
- 43
- 41
Here's some fun I had solving something similar in node.
function formatTimeDiff(date1, date2) {
return Array(3)
.fill([3600, date1.getTime() - date2.getTime()])
.map((v, i, a) => {
a[i+1] = [a[i][0]/60, ((v[1] / (v[0] * 1000)) % 1) * (v[0] * 1000)];
return `0${Math.floor(v[1] / (v[0] * 1000))}`.slice(-2);
}).join(':');
}
const millis = 1000;
const utcEnd = new Date(1541424202 * millis);
const utcStart = new Date(1541389579 * millis);
const utcDiff = formatTimeDiff(utcEnd, utcStart);
console.log(`Dates:
Start : ${utcStart}
Stop : ${utcEnd}
Elapsed : ${utcDiff}
`);
/*
Outputs:
Dates:
Start : Mon Nov 05 2018 03:46:19 GMT+0000 (UTC)
Stop : Mon Nov 05 2018 13:23:22 GMT+0000 (UTC)
Elapsed : 09:37:02
*/
You can see it in action at https://repl.it/@GioCirque/TimeSpan-Formatting

- 2,242
- 1
- 16
- 13
The following code worked for me,
function timeDiffCalc(dateNow,dateFuture) {
var newYear1 = new Date(dateNow);
var newYear2 = new Date(dateFuture);
var dif = (newYear2 - newYear1);
var dif = Math.round((dif/1000)/60);
console.log(dif);
}

- 391
- 4
- 6
It works easily:
var endTime = $("#ExamEndTime").val();
var startTime = $("#ExamStartTime").val();
//create date format
var timeStart = new Date("01/01/2007 " + startTime);
var timeEnd = new Date("01/01/2007 " + endTime);
var msInMinute = 60 * 1000;
var difference = Math.round(Math.abs(timeEnd - timeStart) / msInMinute);
$("#txtCalculate").val(difference);

- 422
- 4
- 9
const firstDate = new Date("2023-03-30 18:03:48")
const lastDate = new Date("2023-03-31 10:03:48")
const nbMinuteBetwenToDate = Math.floor((lastDate.getTime() - firstDate.getTime()) / 60000)

- 19
- 1
- 3
-
Please elaborate on how you have answered OP's question and how your code works – Rojo Mar 30 '23 at 13:07
this will work
duration = moment.duration(moment(end_time).diff(moment(start_time)))

- 133
- 1
- 4