-
Notifications
You must be signed in to change notification settings - Fork 27.5k
1.3.0 input[date] model is a Date object in local time zone instead of UTC #8447
Comments
@estekhin - Thanks for reporting this issue. Has this ever worked in a previous version of AngularJS? |
I believe this is a valid issue and can be fixed by changing the following line: https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L1048
to
But I am not sure how this would affect the parsing of other date/time inputs. Some investigation is needed. |
I think that angular 1.2 does not provide special handling for input[date], so in 1.2 ngModel returns the original string value. |
the date inputs are not implemented in 1.2 at all, so there is no use backporting any fixes there. |
@petebacondarwin why simple Check http://plnkr.co/edit/tkAyCBoILXQWvC84U2SV?p=preview for a demo - plain input value and a |
Related to this issue - if i am fine with input[date] "as is" with the string model, will i be able to disable special input[date] handling in angular 1.3? Is this being discussed somewhere or should i raise a new issue/feature request for this? |
@estekhin - It would be better if we fixed this but you could create an additional input directive that cleared out the $parsers and adds your own. Something like this:
|
Actually from looking at the |
@tbosch was involved in the original implementation, perhaps he can have a view on this? |
Playing with the http://plnkr.co/edit/tkAyCBoILXQWvC84U2SV?p=preview in IE11 which allows entering arbitrary text into input[date]. Entering invalid "2014-00-00" as input. Entering invalid "2014-33-98" results in similar results, with I see that input[date] and input[time] was discussed #5864 and issues linked from it but it is already closed. What would be a proper way to propose changes to handling of all time- and date-related inputs in 1.3? |
Tried to use 1.3 snapshot in the real code and, as expected, got unspoken number of problems due to Please consider these requests/requirements:
At the moment i am using the following hack to force the input[date] ngModel to remain a
With that hack in place my code seems to work with 1.3 snapshot without any other problems. |
+1 for requests from previous comment. |
Possible solution: there are a dozen of places inside Angular code where the Date constructor with two or more parameters ( replacing all |
Seems like I am a bit slow as Two additional demos: 1.2.21-based: http://plnkr.co/edit/WdNVxblrBPQPiSNTOIPA?p=preview 1.3-based: http://plnkr.co/edit/ja9UjP9rtJanYrJCVxMo?p=preview after some reflection i think that i want two things:
|
Hi @estekhin And in the case where the browser does not support native @estekhin Would that also solve your problem? If this is fixed, would you still want the raw string that the user entered? If yes, when are you then parsing the Date? |
@Blesh You said that you started on implementing a generic date parser / serializer based on |
@tbosch hi, in my particular case I am using plain string input[date] value as is as one of the parameters in the GET/POST requests. It works very good in modern browsers which always return a yyyy-MM-dd string (and not so good in old browsers which treat input[date] as input[text] and return whatever the user entered). assuming that ngModel will be a Date with the proper value, converting it into a string is not a problem. the off-by-one is the real pain because one has to either replace the parser/formatter to "identity" as in the hack above or to correct the Date using the getTimezoneOffset(). Of course it would be better to fix it in inside angular instead of everyone inventing their own ways to deal with it. |
Angular used to use the `new Date(month, date, year)` constructor to create `Date` objects in `input[type=date]` et al, which uses the current timezone of the browser. However, the HTML5 spec defines the timezone for for `input[type=date]` to be UTC. This commit changes the parsing to always use the UTC timezone. Closes angular#8447.
@tbosch - I can look, but I seriously doubt I still have that sitting around. It was a standalone module I made in a plnkr almost a year ago. The basics of it was it was examining the locale strings and creating Regexps from them, which were then used to test and parse the date strings. |
Wow! I found it... Here's a Gist I pulled from the original Plunker: |
I have no idea what state it's in, but as I recall I had it mostly or partially working at some point. There are definitely better ways to do this if you were to build something into Angular core. Also, it could be leveraged in those date/time related directives I added a while back. |
@Blesh @tbosch we've got a similar parser as part of https://github.com/angular-ui/bootstrap here although it handles dates only. I think that the whole AngularJS ecosystem would benefit from a standalone / published / maintained parsing routine and I could spend some time collaborating on this. Anyone interested in joining efforts? |
I might be totally wrong here, but it seems to me that using |
Hmmm, on second thought it might be easier than I thought. I'll try to look into that. |
@shahata input[type].valueAsDate: |
Those browsers do not support date/time inputs afaik, so I guess that's okay... |
Angular used to always use the browser timezone for `dateFilter`. An additional parameter was added to allow to use `UTC` timezone instead. Related to angular#8447.
Angular used to always use the browser timezone when parsing `input[date]`, `input[time]`, … The timezone can now be changed to `UTC` via `ngModelOptions`. Closes angular#8447.
Hi, Via that, people can continue to get a Making this configurable is good as I can see usecases for both timezones... Updates the attached PR as well. |
Angular used to always use the browser timezone when parsing `input[date]`, `input[time]`, … The timezone can now be changed to `UTC` via `ngModelOptions`. Closes angular#8447.
Ah, and decided not to include the generic date parser into Angular core... |
The generic date parser is a separate issue for another time. I agree it shouldn't be in core. Most people won't need it. For those that do, though, perhaps a separate module would do? Although at that point, arguably you could just use Moment. |
Regarding why we use the browser timezone as default:
So apparently browsers report the date and time for Thinking about building an app where a user enters a date and time, it feels more natural to assume that he enters that time and date in his local date and time. |
Please reconsider using UTC for both input[date] and input[time] for consistency. Using UTC for input[date] and local timezone for input[time] will be surprising:
|
Angular used to always use the browser timezone for `dateFilter`. An additional parameter was added to allow to use `UTC` timezone instead. Related to angular#8447.
Angular used to always use the browser timezone when parsing `input[date]`, `input[time]`, … The timezone can now be changed to `UTC` via `ngModelOptions`. Closes angular#8447.
Sorry, I did not make this clear: We are using local timezone for all date/time related widgets now, but it can be overridden using |
Angular used to always use the browser timezone when parsing `input[date]`, `input[time]`, … The timezone can now be changed to `UTC` via `ngModelOptions`. Closes angular#8447.
Angular used to always use the browser timezone for `dateFilter`. An additional parameter was added to allow to use `UTC` timezone instead. Related to angular#8447.
BREAKING CHANGE: According to the HTML5 spec `input[time]` should create dates based on the year 1970 (used to be based on the year 1900). Related to angular#8447.
Angular used to always use the browser timezone when parsing `input[date]`, `input[time]`, … The timezone can now be changed to `UTC` via `ngModelOptions`. Closes angular#8447.
…-local]` The HTML5 spec allows to use seconds for `input[time]` and `input[datetime-local]`, even though they are not displayed by all browsers. Related to angular#8447.
I'm not sure if I don't understand or if it doesn't work right for me on Galaxy S3 but for me the string and also Android's input dialogs ar incorrect, even though the browser's locale is correct (alert(new Date()) shows "...GMT+0100 (CET)"). BTW: In another place I have a Date() that stands for a duration and which I build by new Date(dateB-dateA) which then for example outputs "1970-01-01T01:00:00.000Z" which is "correct". But when I use formating "HH:mm" the output is "02:00" instead of "01:00" :/ thx |
I think it's still incorrect that date & time don't have the same format (correct Date Object.) I made a codepen to illustrate. If I set it to today, at 1AM I get different timezones for the 2 inputs, both of which are not UTC.
I can adjust for this by subtracting the timezone offset of each, then adding the local timezone offset when I add the 2 together, but this is incorrect behavior. They should both agree on the timezone. |
@konsumer, in both it is your local timezone with and without daylight saving. This is because the dates are different. We can't really fix this. |
These are just 2 elements in the same page. As you can see in the |
@konsumer if you feel this is still a problem. Please open up a new issue. If you could provide a broken unit test that needs fixing that would be even better. |
Upon more thought, I'm not sure what it should do instead. I think I initially misunderstood what @gkalpak was saying. The reason the timezone is different is that pacific changes based on the time of the year (not just location) so, January 1st (1970) is a different timezone. If I want to use it as a simple addition of milliseconds, I can set the timezone of the time-input to UTC, or adjust it after (as I did in codepen,) I guess I expected the default to be UTC for time, so it would add correctly, but I think I understand why it's not. I will just remember this gotcha, in the future, and set my time-input zones to UTC or process them after. |
Please check this plunker for a demonstration.
Assuming the computer time zone is not UTC and the browser supports input[date], select a date from the input[date] dropdown.
The input field will display a date in the localized format, for example "01.08.2014" in my case. My current timezone is GMT+4.
value is
but expected is either
or
It seems that the code that converts
input[date].value
into aDate
model object first obtains the correct value"2014-07-31T20:00:00.000-0400"
but then replaces the correct"-0400"
suffix with incorrect"Z"
, or it is a matter of formatting when the date is formatted as localized but the timezone is not part of the format and instead the "Z" suffix is hardcoded..The code that converts the
string
value of the input[date] into aDate
model should follow the HTML5 input[type=date] guidelines and convert the value string as if it is the start of the day in the UTC regardless of the current timezone.The text was updated successfully, but these errors were encountered: