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?<
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
I highly recommend using the excellent datejs framework to easily do all your date time calculations
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");
}
I think jQuery EasyDate is exactly what you're looking for.
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.
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