bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

Datepicker popup timezone interpreted incorrectly

Open dotcom9 opened this issue 12 years ago • 49 comments

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?

dotcom9 avatar Apr 16 '14 15:04 dotcom9

+1

jacqueslareau avatar May 13 '14 15:05 jacqueslareau

+1

bernhard-hofmann avatar Jul 02 '14 15:07 bernhard-hofmann

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

weberste avatar Jul 17 '14 14:07 weberste

+1 @weberste thanks for the gist - works perfectly!

bikezilla avatar Aug 07 '14 04:08 bikezilla

+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!

Squiggle avatar Aug 07 '14 11:08 Squiggle

+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

Zacharias3690 avatar Aug 14 '14 20:08 Zacharias3690

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

StevenClontz avatar Aug 14 '14 21:08 StevenClontz

+1

Should mention that this problem isn't only on the datepicker POPUP. It's with the datepicker in general.

prateem avatar Aug 20 '14 06:08 prateem

+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")

xavier-rodet avatar Aug 29 '14 13:08 xavier-rodet

@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 - - == +

stevebrowne avatar Sep 09 '14 11:09 stevebrowne

+1

Urigo avatar Oct 05 '14 21:10 Urigo

+1

cladera avatar Oct 08 '14 11:10 cladera

+1

dherik avatar Oct 15 '14 16:10 dherik

+1

lyadeski avatar Oct 20 '14 15:10 lyadeski

+1

sricc avatar Oct 22 '14 16:10 sricc

+1

damianog avatar Oct 23 '14 05:10 damianog

Yes this please +1

starf avatar Nov 04 '14 16:11 starf

+1!!

HeIsHuaxi avatar Nov 04 '14 23:11 HeIsHuaxi

+1

ThomDevine avatar Nov 06 '14 18:11 ThomDevine

+1

benbartling avatar Nov 10 '14 03:11 benbartling