Anupam
Anupam

Reputation: 1841

Javascript date daylight savings issue

I am facing an issue with daylight savings in javascript while working with dates. The country concerned is UK.

Earlier, before 31st March, the following code gave accurate date:

var caseClosed = new Date(now.getUTCFullYear(), now.getUTCMonth(), 
now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());

So, when I used this for differentiating between two dates, the date retrieved by above code and another date (which is from a legacy system which is storing correct date as per system date time), I got a positive correct value.

But after that date, say today, whenever I use the above code, I am getting an hour less; which is resulting in negative value between the two dates.

Say, for example if the time is 18:06, I am getting it as 17:06 by using the above code. So I tried using the following code and it gives me correct time now:

var caseClosed = new Date(now.getFullYear(), now.getMonth(), 
now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds());

Can anyone please explain what is happening and if the above code (without using UTC thing) will work even when DayLight savings is off by not giving an extra hour (60 mins)?

Upvotes: 0

Views: 754

Answers (1)

Laurent S.
Laurent S.

Reputation: 6946

Well, the UTC date is different from the system date. While the system date is impacted by daylight changes, the UTC is not.... Note that if you wish to use this value to store anything on the server, you should use server time, not client time...

Upvotes: 1

Related Questions