Monday, February 1, 2010
at
9:57 PM
Labels:
Javascript,
Programming
Posted by
bayasaa
Is Javascript calculating day differences wrong? That's because you also need to add local time settings to you calculation.
This function does exactly what you want.
function days_between(date1, date2) {
var ONE_DAY = 1000 * 60 * 60 * 24;
var timeZoneOffset = new Date().getTimezoneOffset()
var date1_ms = date1.getTime() - timeZoneOffset*60*1000;
var date2_ms = date2.getTime() - timeZoneOffset*60*1000;
var difference = parseInt( date1_ms / ONE_DAY) - parseInt( date2_ms / ONE_DAY );
return difference;
}
And you use like this.
alert( days_between( new Date("2010/02/05 12:00"), new Date("2010/02/04 23:00") ));
Be happy and do less.
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment