Datepicker popup timezone interpreted incorrectly
This may be related to https://github.com/angular-ui/bootstrap/issues/1031, but I'm not convinced it's exactly the same, so here's a fresh issue.
When you supply the datepicker popup with an ISO date with no timezone information, it interprets it as a UTC date, with the result that it displays the date incorrectly if the browser timezone is not also UTC.
For example, for a browser in EST (GMT-5) if the popup is given date "1980-03-03T00:00:00", the date displayed is 1980-03-02. Presumably it internally translates the supplied date into local time ("1980-03-02T19:00:00 EST").
As #1031 points out, an ISO date with no timezone information should be treated as a local date, so no adjustment should be made.
Although arguably this should be the default behaviour of the datepicker popup, I was hoping that as a workaround, I would be able to adjust the input using a custom formatter, in the same way that custom parsers are used to adjust the output value, However, when a custom formatter is applied, the popup behaviour is the same. Even though the formatter is called, It appears that the popup uses the raw model value rather than the formatted value.
This can be seen in this plunker; the formatter returns a random hard-coded date value ("01/01/1900") which is applied to the input element. Open the popup and see that the date has been set from the model value, ignoring the formatter. Set your browser PC to EST timezone and see that the popup is set to the day before the model value.
http://embed.plnkr.co/1r2i2IVKQVszXQ4IQXMl/preview
Is there any way the input value to the popup can be intercepted and modified to avoid this behaviour?
+1
+1
I'm using a directive with a formatter and parser that works around the problem: https://gist.github.com/weberste/354a3f0a9ea58e0ea0de
Fundamentally, I believe it's not the datepicker that does the internal translation but the JavaScript Date (which the datepicker uses as a model).
+1 @weberste thanks for the gist - works perfectly!
+1 for this. Whilst it's not technically a bug (it works as specified), it doesn't work as most people would expect. Would be great to have an option to force the datepicker to be backed by a UTC datetime instead of local datetime.
for example and if someone in Germany selects their birthday as 3rd June 1994 it becomes 1994-06-03T00:00Z and not 1993-06-02T23:00Z. This would be incredibly useful, because the server often doesn't know the client's timezone if all dates are supplied UTC without offset, and it makes it very difficult to compensate after-the-fact.
Thanks!
+1 @weberste, @bikezilla Trying to use this directive has the correct model time and the correct time in the input box, however the popup shows the previous day. Is there anything else you had to do to fix the problem? Here's a plunkr demonstrating the issue: http://plnkr.co/edit/sFhA1LwfupvIK85eGPXp?p=preview for reference I'm using central time
I'm working with a JSON object sent by a Rails server which includes a Date rendered as 'yyyy-MM-dd' which has this same issue since Javascript first interprets the string as midnight UTC, then converts to my local time zone:
> new Date('2014-08-14')
Wed Aug 13 2014 19:00:00 GMT-0500 (CDT)
It would be very beneficial if we could either choose the timezone Datepicker uses, or if Datepicker could parse date strings like Angular's own date filter: https://github.com/angular/angular.js/pull/1782
IgorMinar commented on Jan 9, 2013 previously we were always parsing the string input as UTC which cased issues like:
{{ '2012-04-01' | date:'d MMM yyyy' }} renders as 31 Mar 2012
BREAKING CHANGE: string input without timezone info is now parsed as local time/date
+1
Should mention that this problem isn't only on the datepicker POPUP. It's with the datepicker in general.
+1 @weberste solution's is very very helpfull !!!!
@Zacharias3690 You need to use the directive with "datepicker-localdate" instead of "datepicker-local-date" (because the directive name is "datepickerLocaldate")
@Zacharias3690 what worked for me (in GMT) was to change the second setMinutes calc to be minus not plus:
// called with a 'yyyy-mm-dd' string to format
ngModelController.$formatters.push(function (modelValue) {
if (!modelValue) {
return undefined;
}
// date constructor will apply timezone deviations from UTC (i.e. if locale is behind UTC 'dt' will be one day behind)
var dt = new Date(modelValue);
// 'undo' the timezone offset again (so we end up on the original date again)
dt.setMinutes(dt.getMinutes() - dt.getTimezoneOffset());
return dt;
});
This works for me as the timezone offset was a negative number, so - - == +
+1
+1
+1
+1
+1
+1
Yes this please +1
+1!!
+1
+1