md-datepicker sending the day before the one that I select
Here's a codepen
When you first open the page, the date is correct - for example "2015-09-15T08:19:32.808Z" (today is 09-15)
But as soon as you select 9/13/2015 for example, it shows 1 day before "2015-09-12T21:00:00.000Z"
It seems like it's choosing 1 day before the one that I selected It happens for every single date that you choose.
After reading the source code and debugging with a fellow colleague:
Location: We are in Beirut, Lebanon (GMT+3). This is the main cause of the issue.
So when I select 9/13/2015 it shows 9/12/2015 with time 21:00:00.
So this is most likely a timezone issue.
If you are trying this issue from the U.S., you should probably see the date correct, but the time wrong, something like: 9/13/2015 with time: 06:00:00
It really depends on your timezone
Is anyone able to verify the issue?
@jadjoubran Do you see this issue on master?
#4215 related
@jelbourn thanks for helping out! Yes still happening on master. I edited the codepen and added {{myDate}} and the same issue is reproducible
@EladBezalel so what would be the solution? Why would moment.js fix it? The fix in the commit is only for "11:00" and "23:00" that's weird..
The earlier fix was under the notion that this was purely a Daylight Saving Time issue, which would be only one hour off. Seems like that's not really the case, though.
Using something like moment.js (or your own date parsing/formatting) fixes this because this bug is caused by the native JS date formatting. Specifically, you can make a Date set to an exact date at hour = 0, minute = 0, seocnd = 0, and have toLocaleString still give you the day before. E.g.:
var d = new Date(2015, 9, 8, 0, 0, 0); // Should be Oct. 8th at midnight
d.toLocaleString(); // Would write Oct. 7th, 11:00pm in some timezones.
@jelbourn Thanks for your answer!
I just ran your code and I got "10/8/2015, 12:00:00 AM"
Not sure what that means..
But don't you think we should aim for a generic solution that works for everyone? Because I'm guessing this is going to be reported by anyone outside of the U.S.
I'd love to work on a PR for this, if you agree. But again I'm not sure how we should fix this
A "real" fix for this would be to do full-blown Date internationalization, which isn't really in the scope of ng-material. I'm going to try to make the default more sensible for more timezones. It will never be perfect, but that's why the support for using your own i18n (like moment.js) is there.
@jadjoubran I've tried reproducing this by setting the time on my machine to Beirut time, but I'm not seeing the issue. If the code snippet above does actually render the correct date, I'm not sure where the bug is.
If you inspect the data-timestamp attribute of one of the date cells and then do new Date(timestamp) and also new Date(timestamp).toLocaleString(), does the result match what is displayed in the calendar?
Also, which browser and OS combination are you using?
@jelbourn I'm using Chrome Version 45.0.2454.99 m on Windows 10.
Indeed it works well if I run new Date(timestamp) and new Date(timestamp).toLocaleString(). In fact both of them work very well.
It seems that the bug is not in the timestamp (sounds odd I know). Can you please try the following scenario, I think this is how you'll be able to replicate the bug:
- Change your timezone back to beirut
- Open my previous codepen
- You should see the correct date
- Select another date, you'll see that it shows the (Day - 1 ) with the current hour
@jadjoubran Tested with on Windows 8 with Chrome 45 with the OS timezone set to Beirut, and I still wasn't able to see the problem.
The bug very likely has to do with the formatDate function in $mdDateLocale. By default, this uses Date.prototype.toLocaleDateString. If this function is actually returning the right string (that should be set to the text input), then I'm baffled. Can you check that the output of formatDate is when you select a date? If that seems right, can you trace through the md-calendar-change handler in datePicker.js and see if there's something going awry there? Thanks so much for the help debugging this.
Hi @jelbourn I tried tracing through this function but using the console, so I was instantiating dates manually and everything was working perfectly. And from this I came to the following conclusion:
https://github.com/angular/material/blob/c2e17ad6ce0a0f16b2169986adb278083ad22eb8/src/components/datepicker/datePicker.js#L264
This is working absolutely fine.. and in fact when I see the error, the input always shows the correct date, but as soon as you render the value in the view or send it to the database or inspect it, you'll see that it's off by 3 hours (because I'm in GMT +3).
So for example, when I get today's Timestamp and I create a new date, I get the following date:
Fri Sep 25 2015 00:00:00 GMT+0300 (Middle East Daylight Time)
When you run .toLocaleDateString() it works fine: it returns 9/25/2015.
So the problem is not with this code, but mainly with where the date is fully stored - so for example when it's rendered in the view you'll see something like this: "2015-09-24T21:00:00.000Z"
So maybe you have an idea where this is happening?
Here's a screenshot that shows the input showing the correct date whereas the view showing the time that is off by 3 hours (because it's trying to convert it to GMT, so it removes 3 hours).

Also notice how the AngularJS inspect plugin shows the correct date with GMT
@jadjoubran it sounds like, while the datepicker treats dates as local time, some other part of your application is treating them as UTC. Does that sound right?
100% - That's true I am online, let me know if I can help with the debugging process
@jadjoubran If other parts of your app are interpreting the date as UTC, does that means the datepicker itself is always internally consistent? That is, if you click a date in the calendar, that matches the date that renders in the text input?
Yes, it always does
Based on that, I this doesn't sound like a bug in the datepicker itself. As it is now, the fact that it always deals with local times is working as intended; your app can take that local time and do any necessary conversions to UTC.
We do want to add a "UTC mode" to the datepicker, but it's not high on the short-term priority list. Going to close this issue for now- feel free to update if there's something I'm missing.
Hi @jelbourn @jadjoubran,
I am facing the same issue. Is this issue fixed? Or if you could you kindly suggest some work around that works? Cause the date picker is setting my model to one day previous to the selected date.
Thanks.
@prerna12 I wasn't able to fix this yet.. maybe @ThomasBurleson has an idea?
@prerna12 @jadjoubran Based on my understand of this, you need to convert the local value coming from the datepicker to UTC if that is what the rest of your app is using. For any real application, you should be using something like moment.js with $mdDateLocaleProvider to do parsing and formatting.
I'm seeing something similar, but it's intermittent. Sometimes the value assigned to the ng-model $scope property is this:
Tue Jun 30 2015 00:00:00 GMT-0500 (CDT)
And sometimes it's this:
Tue Jun 30 2015 23:59:59 GMT-0500 (CDT)
Same component, same page load. Just clicking the same date over and over again produces mostly the first, but about 1/10 of the time the second.