Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts
Monday, February 1, 2010
at
9:57 PM
Labels:
Javascript,
Programming
Posted by
bayasaa
0
comments
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:
Posts (Atom)