angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

1.3.0 input[date] model is a Date object in local time zone instead of UTC

Open estekhin opened this issue 12 years ago • 43 comments

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

"2014-07-31T20:00:00.000Z"

but expected is either

"2014-07-31T20:00:00.000-0400"

or

"2014-08-01T00:00:00.000Z"

It seems that the code that converts input[date].value into a Date 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 a Date 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.

estekhin avatar Aug 01 '14 14:08 estekhin

Okay, looking over this, there are two important methods: parse and createPatternMatcher... the usage was meant to be generic, parse(somePattern, someDateString)

benlesh avatar Aug 15 '14 19:08 benlesh

So in previous comments:

Why not use new Date(string)?

Because it doesn't parse ISO strings properly in every browser Angular must support. Believe me, I'd rather not have written all of the date parsing stuff.

Replace new Date(y, m, , , , ) with new Date(Date.UTC(y, m, , , , ))

Seems like a fine solution. I feel like I did try that and it broke other tests, but I'm unsure.

benlesh avatar Aug 15 '14 19:08 benlesh

Parsing localized date-time string, especially with localized (non-English) names for day-of-week and month, is a project by itself. Are you really sure that you need it in the core?

estekhin avatar Aug 15 '14 19:08 estekhin

@estekhin That date parser hack I posted is really only missing the day of week bit, it looks like. Either way, that data all already exists in the various localization files in Angular.

Given that those same localization files contain mappings for where they fit into strings, it won't be too difficult to handle with Regexp.

So the only new bits that would have to reside in core would be the parser and pattern match creator logic.

benlesh avatar Aug 15 '14 19:08 benlesh

... actually, worrying about parsing the day of the week is a red herring.

The day of the week is useless unless there is an accompanying numeric day. Which that aforementioned parser already handles.

benlesh avatar Aug 15 '14 19:08 benlesh

@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?

pkozlowski-opensource avatar Aug 16 '14 09:08 pkozlowski-opensource

I might be totally wrong here, but it seems to me that using element.valueAsDate will save a whole lot of effort here (assuming it works correctly in all supported browsers). The problem is that ngModel is really tightly coupled with element.value, so I don't know if it is worth the effort...

shahata avatar Aug 16 '14 09:08 shahata

Hmmm, on second thought it might be easier than I thought. I'll try to look into that.

shahata avatar Aug 16 '14 09:08 shahata

@shahata input[type].valueAsDate: Chrome 36 - supported, returns proper value FireFox 31 - returns "undefined" IE 11 - returns "undefined"

estekhin avatar Aug 16 '14 10:08 estekhin

Those browsers do not support date/time inputs afaik, so I guess that's okay...

shahata avatar Aug 16 '14 11:08 shahata

Hi, ok, thought about this for a while with the following conclusion: Lets parameterize the timezone!

Via that, people can continue to get a Date instance in the browser timezone, but they can change it to use UTC via ngModelOptions. I also added a timezone parameter to the dateFilter that allows to specify UTC as timezone.

Making this configurable is good as I can see usecases for both timezones... Updates the attached PR as well.

tbosch avatar Aug 25 '14 18:08 tbosch

Ah, and decided not to include the generic date parser into Angular core...

tbosch avatar Aug 25 '14 18:08 tbosch

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.

benlesh avatar Aug 25 '14 18:08 benlesh

Regarding why we use the browser timezone as default:

  • input[time] does no timezone conversion on Chrome and iOS, the major browsers that support this feature (see http://caniuse.com/#feat=input-datetime). I.e. if a user enters 11:10 am in the UI, then input.value will be 11:10. This means that those browsers do not convert the time to UTC, so using the browser timezone to create the Date object is correct for them. Same is true for input[date].
  • input[datetime] is not supported in Chrome and neither on iOS, only input[datetime-local] is.

So apparently browsers report the date and time for input.value in the browser timezone, and not in UTC. However, they do assume UTC timezone when reading input.valueAsDate.

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.

tbosch avatar Aug 26 '14 00:08 tbosch

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:

  • one input type returns a value in UTC, the other in random timezone.
  • one input type follows HTML spec, the other does not.
  • if or when browsers will support .valueAsDate and .valueAsNumber properties, the angular model for one input will correspond to these values, the other will not.

estekhin avatar Aug 26 '14 03:08 estekhin

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 ngModelOptions.timezone.

tbosch avatar Aug 26 '14 16:08 tbosch

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)"). But the unformatted date output still says ".....000Z" and the HTML input shows 2015-03-04 instead of 04.03.2015 with ng-model-options="{timezone: 'UTC'}" (tried GMT+0100 too). In the Android's date selector dialog the date is correctly formatted but in both date and time selector dialogs, the "Clear" button is in english instead of german and in the time selector dialog, the time selection goes from 1 to 12 with AM/PM instead of 0 to 23 without AM/PM.

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

fhofer avatar Mar 04 '15 12:03 fhofer

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.

Wed Jun 01 2016 00:00:00 GMT-0700 (Pacific Daylight Time) 
Thu Jan 01 1970 01:00:00 GMT-0800 (Pacific Standard Time) 

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 avatar Jun 02 '16 00:06 konsumer

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

gkalpak avatar Jun 02 '16 08:06 gkalpak

These are just 2 elements in the same page. As you can see in the valFixed code, if I strip the zone offset from both, then add it from new Date().getTimezoneOffset() * 60000 it works fine, but I shouldn't have to do this. The date objects attached to the ng-model of date and time inputs shouldn't default to different timezones.

konsumer avatar Jun 02 '16 10:06 konsumer