I have a web page with three dropdowns for day, month and year. If I use the JavaScript Date
constructor that takes numbers, then I get a Date
object for my current timezone:
new Date(xiYear, xiMonth, xiDate)
Give the correct date, but it thinks that date is GMT+01:00 due to daylight savings time.
The problem here is that I then pass this Date
to an Ajax method and when the date is deserialised on the server it has been converted to GMT and so lost an hour which moves the day back by one.
Now I could just pass the day, month, and year individually into the Ajax method, but it seems that there ought to be a better way.
The accepted answer pointed me in the right direction, however just using setUTCHours()
by itself changed:
Apr 5th 00:00 GMT+01:00
to
Apr 4th 23:00 GMT+01:00
I then also had to set the UTC date, month and year to end up with
Apr 5th 01:00 GMT+01:00
which is what I wanted.
using
.setUTCHours()
it would be possible to actually set dates in UTC-time, which would allow you to use UTC-times throughout the system.You cannot set it using UTC in the constructor though, unless you specify a date-string.Using
new Date(Date.UTC(year, month, day, hour, minute, second))
you can create a Date-object from a specific UTC time.for all location offset Wiki List of UTC time offsets
This worked for me. Not sure if it is a good idea though.
The easiest way that I have found to get the correct date is using datejs.
http://www.datejs.com/
I get my dates via Ajax in this format as a string: '2016-01-12T00:00:00'
Console will read:
Mon Jan 11 2016 19:00:00 GMT-0500 (Eastern Standard Time)
Tue Jan 12 2016 00:00:00 GMT-0500 (Eastern Standard Time)
https://jsfiddle.net/vp1ena7b/3/
The 'addMinutes' comes from datejs, you could probably do this in pure js on your own, but I already had datejs in my project so I found a way to use it to get the correct dates.
I thought that this might help someone...
I don't believe this is possible - there is no ability to set the timezone on a Date object after it is created.
And in a way this makes sense - conceptually (if perhaps not in implementation); per http://en.wikipedia.org/wiki/Unix_timestamp (emphasis mine):
Once you've constructed one it will represent a certain point in "real" time. The time zone is only relevant when you want to convert that abstract time point into a human-readable string.
Thus it makes sense you would only be able to change the actual time the Date represents in the constructor. Sadly it seems that there is no way to pass in an explicit timezone - and the constructor you are calling (arguably correctly) translates your "local" time variables into GMT when it stores them canonically - so there is no way to use the
int, int, int
constructor for GMT times.On the plus side, it's trivial to just use the constructor that takes a String instead. You don't even have to convert the numeric month into a String (on Firefox at least), so I was hoping a naive implementation would work. However, after trying it out it works successfully in Firefox, Chrome, and Opera but fails in Konqueror ("Invalid Date") , Safari ("Invalid Date") and IE ("NaN"). I suppose you'd just have a lookup array to convert the month to a string, like so:
This answer is tailored specifically to the original question, and will not give the answer you necessarily expect. In particular, some people will want to subtract the timezone offset instead of add it. Remember though that the whole point of this solution is to hack javascript's date object for a particular deserialization, not to be correct in all cases.