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.
This is BEST solution
Using:
Code:
Coffee version:
I know this is old but if it helps you could use moment and moment time zone. If you haven't seen them take a look.
http://momentjs.com/timezone/
http://momentjs.com/
two really handy time manipulation libraries.
I believe you need the createDateAsUTC function (please compare with convertDateToUTC)
any mileage in
This may help someone, put UTC at the end of what you pass in to the new constructor
At least in chrome you can say
var date = new Date("2014-01-01 11:00:00 UTC")