Monday, February 1, 2010
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.
This is my first post and maybe I supposed to write things about purpose of this blog and more.
But do less. That is what this blog for.