最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to avoid time zone issues with JQuery datepicker - Stack Overflow

programmeradmin5浏览0评论

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
Add a comment  | 

2 Answers 2

Reset to default 17

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

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
发布评论

评论列表(0)

  1. 暂无评论