-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue when parsing a date in western timezones #138
Comments
I'm also struggeling with the same problem which seems to be that since Pikaday is using the Javascript date object the entered date will be converted to local time zone. I thought I could get past this by using moment.js:
This is because of Pikaday is using the Date object I would assume. Don't know how to get around this. I've tried Jquery.UI.datepicker and it has the same problem. (The reason why Pikaday doesn't detect moment.js might be that you aren't loading moment.js before you load pikaday.js?) |
My workaround is now to set the local timezone when setting the date to PikaDay:
Have tested this in -1200, +1000, +2000, +1200 and it seems to work. |
Yes, Pikaday uses local dates not UTC dates. |
The source date IS the correct local date. The problem is that is is displayed as the day before, because JS date object will apparently assume it's UTC. |
I have a fix for this that avoids the inherent javascript timezone problem with the date object. I was selecting a date in a form field but it was giving the previous day's date and a bit of research revealed this was a common problem.
and modify
The callback can take the (output of toString() is like: It's a bit hack-ish (and I'm not a javascript expert so my modification of |
@regebro et al after struggling with this problem myself for several hours it dawned on me that YYYY-MM-DD is in fact ISO-8601. While ISO-8601 specifies that strings without zone specifiers are to be treated as local time Date.parse in javascript deviates from this in ES5 and assumes UTC. ES6 is expected to correct this. In the mean time you can use setMoment to obtain the correct behavior. Note however setMoment has inconsistencies with Date.parse. eg '' is not parsed as Invalid Date. I might open a tick for that. |
An easy workaround is to construct the date using integer fields, e.g. new Date(2015, 5, 16). Note that the month must be zero-based. |
We have date inputs that look like this, which we use Pikaday 1.2.0 with:
I've noticed that Pikaday will parse the date 2014-04-17 and convert it into a JS Date object. But if you have a timezone that is west of UTC, it will intepret it as 2014-04-17 00:00, UTC, and promptly fill in the field with "Wed Apr 16 2014". Saving the form will submit 2014-04-16 and the date will change.
This has been replicated on IE10 and FF28.
One solution could be to change the line
date = new Date(Date.parse(opts.field.value))
to
date = new Date(Date.parse(opts.field.value) + 43200000)
To add 12 hours to the date. But it is a bit hackish, and there are UTC-13 timezones out there where it still would not work.
(We do use moment.js as well, but for some reason Pikaday doesn't seem to detect it).
The text was updated successfully, but these errors were encountered: