Can any one suggest a jQuery plugin for calculating the difference between two dates (dates may contain time also) and show it as '32 days', '13 hours', '20 min' etc?
-
2Why jQuery hen this can be done with native JavaScript? – Caspar Kleijne May 01 '11 at 07:47
-
possible duplicate of [What's the best way to calculate date difference in Javascript](http://stackoverflow.com/questions/327429/whats-the-best-way-to-calculate-date-difference-in-javascript) – Caspar Kleijne May 01 '11 at 07:54
-
and duplicate of http://stackoverflow.com/questions/41948/how-do-i-get-the-difference-between-two-dates-in-javascript – Dan Apr 04 '13 at 12:36
5 Answers
You can add, subtract and do many other things with the native JavaScript Date object. It's powerful enough for most of your needs. If using it, you save kilobytes of page size and make the code understandable for everyone (probably 90% of JavaScript developers have never used any plugins to calculate dates)
The thing I hate about Date object is it does not have a built-in formatted output.
For example, you cannot tell the localized day of the week or month name without string parsing. Then datejs comes to help you.
What can a Date object do?
var msMinute = 60*1000,
msDay = 60*60*24*1000,
a = new Date(2012, 2, 12, /* days, hours*/ 23, 59, 59),
b = new Date("2013 march 12"), /* string */
c = new Date(), /* now */
d = new Date(c.getTime() + msDay - msMinute); /* tomorrow - minute */
console.log(a.getUTCHours());
console.log(typeof (b - a + 1000));
console.log(Math.floor((b - a) / msDay) + ' full days between');
console.log(Math.floor(((b - a) % msDay) / msMinute) + ' full minutes between');
console.log('Today is ' + c.getDay() + ' day of week');
console.log('Tomorrow is ' + d.getDay() + ' day of week');
console.log('Your timezone offset is ' + c.getTimezoneOffset() + ' minutes');
Easily calculate days till Christmas
And, sometimes there is more truth in a joke then you could ever expect

- 535
- 1
- 17
- 39

- 55,715
- 40
- 116
- 154
-
If adding a number to date always use `getTime()`. This code does not work `console.log(a + 10)` and this works `console.log(a.getTime() + 10)` – Dan Apr 04 '13 at 12:34
Here's a pretty simple Javascript implementation I just hacked up. You can do the math to extend this to months or years or remove the plurals for 1 values if needed.
var dateDiff = function ( d1, d2 ) {
var diff = Math.abs(d1 - d2);
if (Math.floor(diff/86400000)) {
return Math.floor(diff/86400000) + " days";
} else if (Math.floor(diff/3600000)) {
return Math.floor(diff/3600000) + " hours";
} else if (Math.floor(diff/60000)) {
return Math.floor(diff/60000) + " minutes";
} else {
return "< 1 minute";
}
};
dateDiff(new Date(1990, 1, 1), new Date(1990, 1, 13)) // -> 12 days

- 49,587
- 11
- 107
- 104
I highly recommend using the excellent datejs framework to easily do all your date time calculations

- 42,480
- 25
- 113
- 143
-
date.js never got past Alpha-1 and is outdated since 2007. I would very much discourage using it! – Stacky Aug 16 '18 at 12:57
I think jQuery EasyDate is exactly what you're looking for.

- 2,481
- 15
- 15
-
1Thanks, confirming that this worked for me and indeed will achieve what was asked in question. Plenty of examples provided. – Dominor Novus Oct 18 '12 at 12:39
-
-
day1= $("#tMTLCLsDay").val();
month1= $("#tMTLCLsMnth").val();
year1= $("#tMTLCLsYr").val();
hour1= $("#tMTLCLs_HOUR").val();
min1= $("#tMTLCLs_MIN").val();
var Date1=month1+ "/" + day1+ "/" + year1+ " " + hour1 + ":" + min1+ ": 00" ;
day2= $("#tMTLCLsDay").val();
month2= $("#tMTLCLsMnth").val();
year2= $("#tMTLCLsYr").val();
hour3= $("#tMTLCLs_HOUR").val();
hour2= $("#tMTLCLs_MIN").val();
var Date2=month2+ "/" + day2+ "/" + year2+ " " + hour2 + ":" + hour2 + ": 00" ;
if(Date.parse(Date1)<Date.parse(Date2))
{
alert("Date 2 is large");
}