-
Notifications
You must be signed in to change notification settings - Fork 1
Wrong Date representation #132
Comments
If you use the JavaScript Date object, I recommend you to use the representation "YYYY-MM-DDTHH:MM:SS" in order to avoid issues with time zones (the T is important). If you create a Date instance with a string like "2018", it might be possible that the browser uses "2018-01-01 00:00:00 CET" as entry string. But when you print out a string again, it might use UTC, resulting in "2017-12-31 23:00:00 UTC". In my eyes, the representation is correct in Chrome and Firefox, but wrong in Safari. Try this in the browsers:
|
As Andreas says, this is a time zone issue. |
@andreas-aeschlimann well I am puzzled. If it is timezone problem, how can be that for me safari is showing correct date but chrome not but for you it is other way around? shouldn't the problem be consistent for everyone w.r.t browser type? |
@SepidehAlassi it is the other way around. See the following console output: Chrome:
Safari:
As you can see, Safari doesn't even accept the standard ISO datetime. Adding the "T" as I said gives you back a wrong time in Safari. I entered 00:00:00 and when I print it I get another time, in fact 01:00:00. This can be fixed in the following way: Add a "Z" to the datetime string as follows:
This will consistently give the same time accross all browsers. However, the return value is 01:00:00 which is not want you probably want. Hence the fix as I posted above:
The implementation of course depends on how the database/server is configured and how you want to store and represent times. |
I think we have to refactor the whole date component. At the moment, we are dealing with strings and specific Date type that makes very difficult to change the timezone. |
@flavens okay. From my experience I can recommend moment.js, it takes care of all these annoying and nasty conversions. There is also a wrapper for Angular at https://github.com/urish/ngx-moment. |
Please keep in mind that we are using the DateAdapter for |
@andreas-aeschlimann Well, I tried your example in both Safari and Chrome, the result is very interesting
Safari:
As you see both safari and chrome give me the correct time because I am in GMT time zone. It seems to me that the conversion from GMT to central European time in safari is not correct! |
@andreas-aeschlimann I also tried this, look at the result
Chrome:
They also both return 00:00:00 GMT! |
@SepidehAlassi indeed, I think this is correct. Using Z you always get GMT, which is the correct time in your case right now. But to make sure time zones are reflected in the return string (if needed?), we might have to fix it with the offset function. |
One of the problems seems a breaking change in EcmaScript 6. Chrome has already implemented it, others haven't. This makes time handling a nightmare without a package as MomentJS. This post summarizes it nicely: TLDR: There is no consistent way of creating JS Date objects at the moment because browsers treat date strings differently. |
Browser based problem with date representation. see
https://github.com/dhlab-basel/beol/issues/75
Date is shown correctly in Safari, but incorrectly in Chrome!
The text was updated successfully, but these errors were encountered: