eight04 / angular-datetime

A directive to add the behavior of datetime input on unsupported browsers.
MIT License
93 stars 28 forks source link

Error parsing (w3c formatted) datetimes having timezone different than the datetime parser one #49

Closed marechalsamuel closed 6 years ago

marechalsamuel commented 7 years ago

I can't get to parse some w3c formatted dates properly, If the timezone is +01:00, it seems to go wrong and I get the date of the day. Here is an example : https://plnkr.co/edit/g2tPcae2mfdp3PHo9HZ1?p=preview

marechalsamuel commented 7 years ago

After searching and testing, it seems parsing gets an error on timezone whenever it's not the default timezone or the one that is set. Getting this error message : Pattern value mismatch Here is the test : https://plnkr.co/edit/SBZDc5CaaFLsJ9eKzuAE?p=preview

eight04 commented 7 years ago

I'm not sure what you are going to achieve.

If you want to use 1976-11-14T07:05:58+01:00 as default value, you can convert the date string into a date object: https://plnkr.co/edit/9YRgQClGD9gpVQSeLlzT?p=preview

If you want to create a datetime input with specific timezone (+0100), currently you can't. There is only a datetime-utc flag to use +0000 as timezone.

And here is why you got the date of the day:

  1. The parser raised mismatch error when parsing a date string with incorrect timezone.
  2. The directive tried to revert the input to previous state when getting a parsing error.
  3. The date was set to its initial value new Date().
marechalsamuel commented 7 years ago

Converting my string into a date object will do it. I guess parsing timezone is work in progress, maybe meanwhile it would be convenient to add a note in the readme about ZZ on this point ? Thank you anyway for your reactivity and also for you work !

eight04 commented 7 years ago

There were some discussions to make timezone "editable", but this feature is rarely used. Most of the time we only save the date into database without timezone information, and display the date to users in their local timezone. If timezone information is needed, we use another field to record it.

However, it would be good if we can make timezone "parsable", which should work like this:

datestr = "1976-11-14T07:05:58+01:00";
parser = datetime("yyyy-MM-ddTHH:mm:ssZZ");
date = parser.parse(datestr, true).getDate();   // a flag allowing the parser to switch the timezone automatically

date.getTime() == new Date(datestr).getTime();  // true
parser.inTimezone("+01:00");    // true
marechalsamuel commented 7 years ago

For information, we found a neat solution to handle the issue making a directive with $parsers and $formatters having a priority of 101 (superior to this library)

https://plnkr.co/edit/g2tPcae2mfdp3PHo9HZ1?p=preview

I guess this could be directly done inside the library.

eight04 commented 7 years ago

You will get the date in local timezone with that code, also the colon : is missing.

If timezone doesn't matter, I would do something like this: https://plnkr.co/edit/MRAoBt8Cam6YbmlvHgkb?p=preview

eight04 commented 6 years ago

Now you can use datetime-zone to specify a custom timezone.