Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid time zone issues with JQuery datepicker

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 :)

like image 327
jumps4fun Avatar asked Mar 28 '14 15:03

jumps4fun


People also ask

How do I change the default date in DatePicker?

Syntax: $(". selector"). datepicker( {defaultDate:"+6"} );

How do I set a DatePicker range?

Steps to add Datepicker – $(document). ready(function(){ $("#setMin,#setMax"). datepicker({ dateFormat: "yy-mm-dd" }); $('#datepicker'). datepicker({ dateFormat: "yy-mm-dd", maxDate:'+1m +10d', minDate: -10 }); });

How do I fix my DatePicker position?

To change the position of the jQuery UI Datepicker just modify . ui-datepicker in the css file. The size of the Datepicker can also be changed in this way, just adjust the font size.

How do I limit DatePicker?

You can restrict the users from selecting a date within the particular range by specifying MinDate and MaxDate properties. The default value of MinDate property is 1/1/1920 and MaxDate property is 12/31/2120 . Dates that appears outside the minimum and maximum date range will be disabled (blackout).


1 Answers

I 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

like image 196
jumps4fun Avatar answered Sep 20 '22 07:09

jumps4fun