2 Replies Latest reply on May 8, 2014 6:28 AM by Jump_Over

# Hidden miliseconds

Hi Forum,

Playing with Date objects I faced with something weird. Let say a goal is to calculate a differrence between two dates (how many days).

Since endDate - startDate returns number of difference in miliseconds - I divide this by 24*60*60*1000.

It works... mostly.

Example 1:

factor = 86400000;

startDate = new Date(2013,11,18);

endDate = new Date (2014,2,24);

howManyDays = (endDate - startDate) / factor     //     96 days

Example 2:

factor = 86400000;

startDate = new Date(2013,11,18);

endDate = new Date (2014,3,1);

howManyDays = (endDate - startDate) / factor     //     103.958333333333 days

Example 3:

factor = 86400000;

startDate = new Date(2013,11,18);

endDate = new Date (2014,10,1);

howManyDays = (endDate - startDate) / factor     //     318 days

Any idea why a difference calculated to middle of year (from April to September) is not fitting a multiple of day in miliseconds?

It can be rounded, of course, but...

Jarek

• ###### 1. Re: Hidden miliseconds

Hi Jarek,

There should be a health warning. WARNING: The Date object will give you the screaming heebie-jeebies. It will have you banging your head against the wall begging for mercy.

Anyway, now that we've got that out of the way.

You'll notice that 1 - (1/24) = 0.9583333...

This means that we're an hour short of 1 day in your example.

Why? Because of daylight saving time on your computer.

When you do new Date(), or new Date(x,y,z), you get the date alright, but you also get daylight saving time thrown in for free, according to what your computer thinks the daylight saving time would have been for that date. (Possibly based on this year's settings: so if Daylight savings time changed 1 April this year in your locale, your computer will presume that it changed at the same date 18 years ago too, I think).

What you are NOT getting is UTC time for the supplied date.

So when you subtract two dates (without hours and minutes, the computer presumes I think midnight), the computer will convert each to the number of millisecond from 1 Jan 1970 based on daylight saving time for your locale at midnight on the date specified.

So you can see that you might easily get calculations that are an hour off.

I think that a safe way around this is to get the timezone offset and add that too. For instance, on my computer I get:

new Date(2013,11,18).getTimezoneOffset()

Result: -180

new Date(2013,11,18).getTimezoneOffset()

Result: -120

(Of course, the result is in minutes, so you have to calculate for that).

You will probably get different numbers.

The question is, how safe is this? If you create a database of times which will be read on a computer in a different part of the world, will the times be right?

It requires very careful thought. When you say "2013, 11, 18", what time do you actually mean?

Aaaargh!

Ariel

• ###### 2. Re: Hidden miliseconds

Hi Ariel,

Thx for this explanation!

I didn't consider time zone offset at all. So this is the end when we start to play with the time.

Jarek