I'm using datepicker in an input form, and sending the results through json to a database. I am using this line, to get the date from the datePicker:
date = $("#datepicker").datepicker('getDate');
Now, I would expect this to return 2014-04-03T00:00:00.000Z
But in fact it returns 2014-04-02T22:00:00.000Z
Notice the two hour difference, which unintentionally changes the day of month as well. I have no use for the hours and the smaller time units. However I do want the date to be right, without adding a dreaded +1
to my code. I suspect this has something to do with time zones, but I can't seem to find a solution to it in the documentation, or other Q&A's online. Could anyone point me in the right direction? My time zone is GMT +1 if that matters.
Thanks :)
I'm using datepicker in an input form, and sending the results through json to a database. I am using this line, to get the date from the datePicker:
date = $("#datepicker").datepicker('getDate');
Now, I would expect this to return 2014-04-03T00:00:00.000Z
But in fact it returns 2014-04-02T22:00:00.000Z
Notice the two hour difference, which unintentionally changes the day of month as well. I have no use for the hours and the smaller time units. However I do want the date to be right, without adding a dreaded +1
to my code. I suspect this has something to do with time zones, but I can't seem to find a solution to it in the documentation, or other Q&A's online. Could anyone point me in the right direction? My time zone is GMT +1 if that matters.
Thanks :)
Share Improve this question asked Mar 28, 2014 at 15:37 jumps4funjumps4fun 4,13210 gold badges52 silver badges100 bronze badges 1- is your OS or browsers time zone correctly configured? – Spokey Commented Mar 28, 2014 at 15:44
2 Answers
Reset to default 17I solved this a while ago, but forgot to post an answer.
After retrieving date
, this is how i fixed it:
date.setMinutes(date.getMinutes() - date.getTimezoneOffset());
voilla
I could not figure out what you did there so I came up with a bit of a hackterrific solution.
I took the value of the alt field in UNIX:
$( function() {
$( "#datepicker" ).datepicker({
altField: "#alternate",
altFormat: "@",
});
It came out all sorts of weird with 3 extra 0's and a day behind my time zone. So I figured out the difference and added it on.
var a = document.getElementById("alternate").value; // take alternative field UnixTimeStamp value
a = a.slice(0, -3); // get rid of 3 extra 0's
a = +a + +57000; // convert to Thai time