Can someone explain this to me. From jconsole …
from = new Date('01/01/2010')
Fri Jan 01 2010 00:00:00 GMT-0800 (PST)
thru = new Date('06/07/2010')
Mon Jun 07 2010 00:00:00 GMT-0700 (PST)
(thru - from) / (1000 * 24 * 60 * 60)
156.95833333333334
Why don’t I get a whole number of days? How do I calculate the difference between two dates?
thanks much.
Your first date is coming out as GMT -0800, the second is GMT -0700 – that’s a 1 hour difference, which is 0.041666 of a day – exactly the amount you’re off by.
This may have to do with daylight savings time differences, since one of your dates is in January and the other is in June; thus one would be on daylight savings and the other would be off it. (And GMT -0800 is PST when not on daylight savings; GMT -0700 is PST when on daylight savings.)
You should be safe to simply round to the nearest integral number of days, since daylight savings will never vary by more than an hour in either direction.