You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Datepicker: Incorrect date displayed when selecting a date in the first week of November 1999 #5185
changed the title [-]Datepicker: Incorrect date displayed when selecting a date int he first week of November 1999[/-][+]Datepicker: Incorrect date displayed when selecting a date in the first week of November 1999[/+]on Jun 17, 2017
I forked your plunker and added some locale settings. Try it with your language (don't forget to fix the myParse function): http://plnkr.co/edit/xfab14?p=preview
Hmm, I'm still getting the same behavior, in Chrome and Firefox. What needs to be fixed in the myParse function?
Also, FWIW I tried it in Microsoft Edge 38.14393.0.0 and I get the expected behavior.
myParse function is configured to brazilian portuguese, which uses the day/month/year format. If the format of your language is similar, you have to do nothing.
It's a problem with Daylight Saving Time. The datepicker builds a javascript Date object with the date you picked and set the time to 0:0:0 (in fact, javascript does it when you build a Date object providing just year, month and day).
You can get more details in here: https://stackoverflow.com/a/5860841/6433166
You can solve it overriding the createDate function of the NativeDateAdapter and setting the hours to 12, instead of leaving it to 0:0:0 (midnight).
createDate(year: number, month: number, date: number): Date {
if (month < 0 || month > 11 || date < 1) {
return null;
}
let result = this._createDateWithOverflow(year, month, date);
result.setHours(12); // <- here is what corrects it
if (result.getMonth() != month) {
return null;
}
return result;
}
@fxck , please, take a look at this... is my conclusion right? I mean... is this the right place to do it? Or is it something that should be fixed with a more complex code somewhere else in order to preserve the midnight time in the Date object?
The problem seems to affect some past dates, according to what is writen in ECMA 5.1 specification:
The implementation of ECMAScript should not try to determine whether the exact time was subject to daylight saving time, but just whether daylight saving time would have been in effect if the current daylight saving time algorithm had been used at the time. This avoids complications such as taking into account the years that the locale observed daylight saving time year round.
Basically it seems that it'll calculate past DST dates as they had happened nowadays. Then we have a problem when the DST in the past doesn't follow today's rule (It happened in Brazil in the year of 1984)
@julianobrasil Ah yeah that sounds like it may be the issue, I wonder if I can try converting to UTC dates or something to get around this. I'd like to avoid setting hours since it'll complicate things later when I add a timepicker
This time, I'm not messing with the hours. I think it's working just fine even considering the problematic past DST dates like Brazil's November 7th, 1984 (from 1980 to 1984 we didn't have the usual October-to-February daylight saving time period over here):
I also tried some other timezones (changing my notebook timezone) that are critical from my point of view (GMT:0, GMT:+1200 and GMT:-1200) during standard time and DST. It was not an exhaustive testing series, but it worked with all cases I tested. Also tested with GMT:-0500 (as it seems to be the timezone @willisd2 had the problem)
Edited: But this aproach is not different from setting the hour manually (except for the fact that you can possibly set the hours by a time piker in the future - but in this situation you should take care of the DST again if you wish to avoid the problem. Or... work with UTC dates everywhere in the component, abandoning the Date object - it is a tricky issue, I think).
Hey @julianobrasil I'm facing the same issue right now! It's 17th October 17:54 (GMT-02 because of daylight summer time) right now and when I select a date on DatePicker (using the CustomDateAdapter from http://plnkr.co/edit/jc4Ywu?p=preview) I'm getting the next day (18th October).
I've tried a bunch of other configurations, changing hours, trying with UTC but didn't manage to make it work correctly. Any idea what else I could do?
@murilobd, aparently there's an issue with datepicker and DST in some regions. This seems to be related to Javascript Date object and ECMA 5.1 - so it depends on the browser you're using. Take a look at #7167 (comment).
Got it @julianobrasil ! Thanks for answering!! Last question: how complicated would be to use moment.js as DateAdapter!? Maybe they already have fixed this issue!
Yeah, in moment.js dateadapter this issue is not happening. I've never used it, but if I had problems, I'd use for sure! It doesn't seem a complicated thing. Aparently you'll just have to switch your project Dates to Moment object.
Activity
[-]Datepicker: Incorrect date displayed when selecting a date int he first week of November 1999[/-][+]Datepicker: Incorrect date displayed when selecting a date in the first week of November 1999[/+]julianobrasil commentedon Jun 17, 2017
Seems to be working fine:
willisd2 commentedon Jun 17, 2017
Interesting, might be a locale issue.
julianobrasil commentedon Jun 17, 2017
I forked your plunker and added some locale settings. Try it with your language (don't forget to fix the
myParsefunction): http://plnkr.co/edit/xfab14?p=previewwillisd2 commentedon Jun 17, 2017
Hmm, I'm still getting the same behavior, in Chrome and Firefox. What needs to be fixed in the myParse function?
Also, FWIW I tried it in Microsoft Edge 38.14393.0.0 and I get the expected behavior.
julianobrasil commentedon Jun 17, 2017
myParse function is configured to brazilian portuguese, which uses the
day/month/yearformat. If the format of your language is similar, you have to do nothing.Intriguing behavior you're experiencing...
julianobrasil commentedon Jun 17, 2017
It happens here. I picked a date way before 1999.
julianobrasil commentedon Jun 17, 2017
It's a problem with Daylight Saving Time. The datepicker builds a javascript Date object with the date you picked and set the time to 0:0:0 (in fact, javascript does it when you build a Date object providing just year, month and day).
You can get more details in here: https://stackoverflow.com/a/5860841/6433166
You can solve it overriding the createDate function of the NativeDateAdapter and setting the hours to 12, instead of leaving it to 0:0:0 (midnight).
Take a look at this plunker where I made the little change shown below: http://plnkr.co/edit/shFewF?p=preview
@fxck , please, take a look at this... is my conclusion right? I mean... is this the right place to do it? Or is it something that should be fixed with a more complex code somewhere else in order to preserve the midnight time in the Date object?
mmalerba commentedon Jun 23, 2017
Interesting, I'm not seeing this for 11/4/1999 or 11/6/1984, I wonder if it only happens depending on the time zone you're in or something.
edit: ah, I've managed to repro for 8/14/1800, will investigate
julianobrasil commentedon Jun 23, 2017
@mmalerba, I think the problem hapens when you try to access a date contained in Daylight Summer Time in your locale zone.
julianobrasil commentedon Jun 23, 2017
The problem seems to affect some past dates, according to what is writen in ECMA 5.1 specification:
The implementation of ECMAScript should not try to determine whether the exact time was subject to daylight saving time, but just whether daylight saving time would have been in effect if the current daylight saving time algorithm had been used at the time. This avoids complications such as taking into account the years that the locale observed daylight saving time year round.
Basically it seems that it'll calculate past DST dates as they had happened nowadays. Then we have a problem when the DST in the past doesn't follow today's rule (It happened in Brazil in the year of 1984)
mmalerba commentedon Jun 23, 2017
@julianobrasil Ah yeah that sounds like it may be the issue, I wonder if I can try converting to UTC dates or something to get around this. I'd like to avoid setting hours since it'll complicate things later when I add a timepicker
julianobrasil commentedon Jun 25, 2017
@mmalerba, I tried it with UTC dates and have good news: http://plnkr.co/edit/jc4Ywu?p=preview
This time, I'm not messing with the hours. I think it's working just fine even considering the problematic past DST dates like Brazil's November 7th, 1984 (from 1980 to 1984 we didn't have the usual October-to-February daylight saving time period over here):
I also tried some other timezones (changing my notebook timezone) that are critical from my point of view (GMT:0, GMT:+1200 and GMT:-1200) during standard time and DST. It was not an exhaustive testing series, but it worked with all cases I tested. Also tested with GMT:-0500 (as it seems to be the timezone @willisd2 had the problem)
Edited: But this aproach is not different from setting the hour manually (except for the fact that you can possibly set the hours by a time piker in the future - but in this situation you should take care of the DST again if you wish to avoid the problem. Or... work with UTC dates everywhere in the component, abandoning the Date object - it is a tricky issue, I think).
3 remaining items
functionportal commentedon Jul 14, 2017
This is what happens for ISO date "1948-11-02T04:00:00.000Z"
julianobrasil commentedon Jul 14, 2017
@craftyInstigator, There is a PR to fix it. If you can't wait, try this solution: http://plnkr.co/edit/jc4Ywu?p=preview
murilobd commentedon Oct 16, 2017
Hey @julianobrasil I'm facing the same issue right now! It's 17th October 17:54 (GMT-02 because of daylight summer time) right now and when I select a date on DatePicker (using the CustomDateAdapter from http://plnkr.co/edit/jc4Ywu?p=preview) I'm getting the next day (18th October).
I've tried a bunch of other configurations, changing hours, trying with UTC but didn't manage to make it work correctly. Any idea what else I could do?
julianobrasil commentedon Oct 17, 2017
@murilobd, aparently there's an issue with datepicker and DST in some regions. This seems to be related to Javascript Date object and ECMA 5.1 - so it depends on the browser you're using. Take a look at #7167 (comment).
murilobd commentedon Oct 17, 2017
Got it @julianobrasil ! Thanks for answering!! Last question: how complicated would be to use moment.js as DateAdapter!? Maybe they already have fixed this issue!
julianobrasil commentedon Oct 17, 2017
Yeah, in moment.js dateadapter this issue is not happening. I've never used it, but if I had problems, I'd use for sure! It doesn't seem a complicated thing. Aparently you'll just have to switch your project Dates to Moment object.
murilobd commentedon Oct 17, 2017
Thanks @julianobrasil !! I just created a gist with the MomentDateAdapter I've created for build beta 8 from Angular. If anyone needs: https://gist.github.com/murilobd/605edbc5c668f4f9f943dfb82a7cadd9
julianobrasil commentedon Oct 17, 2017
@murilobd, currently there is an official MomentDateAdapter (that accidentaly wasn't included in beta.12): https://github.com/angular/material2-moment-adapter-builds
murilobd commentedon Oct 17, 2017
@julianobrasil yeap! I found this MomentDatePicker and used! As my application is running on Beta 8, I had to change some code to make it work!
rahimnagori commentedon Jul 10, 2019
This issue only appears if your current timezone is below 00:00 (negative).
julianobrasil commentedon Jul 10, 2019
@rahimnagori, this shouldn't be an issue anymore since #10068, that was merged last year. Have you seen it happen recently?
angular-automatic-lock-bot commentedon Sep 11, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.