Day incorrect in datepicker
Bug, feature request, or proposal:
When I select a date I see the correct date in the field but, when I save, the datepicker send the day before the date I have selected
here's the code:
<md-form-field> <input mdInput [(ngModel)]="fareCalendar.startDate" name="startDate" [mdDatepicker]="picker" placeholder="startDate"> <md-datepicker-toggle mdSuffix [for]="picker"></md-datepicker-toggle> <md-datepicker #picker></md-datepicker> </md-form-field>
I am using angular 4 and @angular/material": "^2.0.0-beta.10

but the date:

How can I send the UTC date
@jt-helsinki @adaojunior, are you in regions with daylight saving time period currently on?
Yes!! Sydney!!
And is it a normal DST period, I mean, every year, in October, Sydney is usually in DST...?
yes, First weekend of Oct to first weekend of April of the next year Sydney DST
@mmalerba, I've heard of something about ECMA 5.1 and previous versions having DST issues... if this is the problem, it would affect older browsers (that, for sure, is not the case of @jt-helsinki's https://github.com/angular/material2/issues/7167#issuecomment-336249082). Look at this test in IE 11 that I couldn't reproduce in Chrome (I think you have already noticed the calendar showing the 31st day of the previous month):

In above image, automatic DST detection is activated. If I disable it the problem doesn't happen.
EDIT: take a look at this old article that I've found: https://codeofmatt.com/2013/06/07/javascript-date-type-is-horribly-broken/
Maybe some polyfills are causing the problem.
Oddly enough, this started happening when the Aussie daylight savings time ticked over.
Strange because he same problem doesn't happen here in Europe. Then again, maybe it will when the clocks change at the end of the month.
Awesome sluething @julianobrasil! I was able to reproduce it with those settings, I'll look into it.
@julianobrasil it looks like that article you linked is spot on. IE seems to be doing its own DST correction, so my logic to do it manually with UTC dates is overcompensating
@jt-helsinki , same thing here in Europe when DST kicked in. If DatePicker is not supposed to handle different timezones, shouldn't it ( at least ) handle DST ?
Indeed it should, hence the open PR to fix it
@mmalerba shouldnt the datepicker not support timezones
its for selecting only date
if i am selecting date of birthday it shouldnt change according to different time zones
I made my self a workaround by overriding the MomentJS DateAdapter compiling the date from UTC and then converting it into the locale date:
import { Inject, Injectable, Optional } from '@angular/core';
import { MAT_DATE_LOCALE } from '@angular/material';
import { MomentDateAdapter } from '@angular/material-moment-adapter';
import { Moment } from 'moment';
import * as moment from 'moment';
@Injectable()
export class MomentUtcDateAdapter extends MomentDateAdapter {
constructor(@Optional() @Inject(MAT_DATE_LOCALE) dateLocale: string) {
super(dateLocale);
}
createDate(year: number, month: number, date: number): Moment {
// Moment.js will create an invalid date if any of the components are out of bounds, but we
// explicitly check each case so we can throw more descriptive errors.
if (month < 0 || month > 11) {
throw Error(`Invalid month index "${month}". Month index has to be between 0 and 11.`);
}
if (date < 1) {
throw Error(`Invalid date "${date}". Date has to be greater than 0.`);
}
let result = moment.utc({ year, month, date }).locale(this.locale);
// If the result isn't valid, the date must have been out of bounds for this month.
if (!result.isValid()) {
throw Error(`Invalid date "${date}" for month with index "${month}".`);
}
return result;
}
}
The important line being:
let result = moment.utc({ year, month, date }).locale(this.locale);
@Silthus Your solution works like a charm. Just had this issue and you commented 18 hours ago. Lucky me! :D
Silthus' workaround works great.
@mmalerba: Is there a plan to implement something into oficial MomentDateAdapter to avoid workarounds?
@MichalK6677 I wasn't aware that the MomentDateAdapter had this issue as well. Do you want to send a PR to fix it?
@mmalerba I can send a PR later this day.
You can change the default behaviour to parse dates as UTC by providing the MAT_MOMENT_DATA_ADAPTER_OPTIONS and setting it to useUtc: true.
@NgModule({ imports: [MatDatepickerModule, MatMomentDateModule], providers: [ { provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS, useValue: { useUtc: true } } ] })
Check here for more details
MAT_MOMENT_DATE_ADAPTER_OPTIONS - oh man, that's so awesome!
However, this..., it's not should, it must be default configuration for datepicker.
Why does datepicker care about the timezones at all? Who would ever need that?
@raibait, in fact, it doesn't care about it. But the underlying objects that currently store the date values usually do (regular javascript Date objects and Moment.js are the most common cases). This is the reason for so many issues related to wrong dates. Building a date adapter and configuring it correctly is important to avoid headaches.