components icon indicating copy to clipboard operation
components copied to clipboard

Day incorrect in datepicker

Open aceleghin opened this issue 9 years ago • 46 comments

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 rtcnm

but the date: f5z10

How can I send the UTC date

aceleghin avatar Sep 19 '17 10:09 aceleghin

@jt-helsinki @adaojunior, are you in regions with daylight saving time period currently on?

julianobrasil avatar Oct 12 '17 22:10 julianobrasil

Yes!! Sydney!!

phmello avatar Oct 12 '17 22:10 phmello

And is it a normal DST period, I mean, every year, in October, Sydney is usually in DST...?

julianobrasil avatar Oct 12 '17 22:10 julianobrasil

yes, First weekend of Oct to first weekend of April of the next year Sydney DST

phmello avatar Oct 12 '17 23:10 phmello

@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):

image

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.

julianobrasil avatar Oct 13 '17 00:10 julianobrasil

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.

jt-helsinki avatar Oct 13 '17 11:10 jt-helsinki

Awesome sluething @julianobrasil! I was able to reproduce it with those settings, I'll look into it.

mmalerba avatar Oct 17 '17 16:10 mmalerba

@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

mmalerba avatar Oct 17 '17 18:10 mmalerba

@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 ?

darkurse avatar Nov 02 '17 11:11 darkurse

Indeed it should, hence the open PR to fix it

mmalerba avatar Nov 02 '17 17:11 mmalerba

@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

ankitraonka avatar Nov 13 '17 14:11 ankitraonka

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 avatar Apr 30 '18 14:04 Silthus

@Silthus Your solution works like a charm. Just had this issue and you commented 18 hours ago. Lucky me! :D

Laurensvdw avatar May 01 '18 08:05 Laurensvdw

Silthus' workaround works great.

@mmalerba: Is there a plan to implement something into oficial MomentDateAdapter to avoid workarounds?

MichalK6677 avatar May 14 '18 17:05 MichalK6677

@MichalK6677 I wasn't aware that the MomentDateAdapter had this issue as well. Do you want to send a PR to fix it?

mmalerba avatar May 15 '18 00:05 mmalerba

@mmalerba I can send a PR later this day.

Silthus avatar May 15 '18 06:05 Silthus

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

glucena avatar Jul 03 '18 08:07 glucena

MAT_MOMENT_DATE_ADAPTER_OPTIONS - oh man, that's so awesome!

However, this..., it's not should, it must be default configuration for datepicker.

smnbbrv avatar Aug 22 '18 16:08 smnbbrv

Why does datepicker care about the timezones at all? Who would ever need that?

raibait avatar Oct 01 '18 15:10 raibait

@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.

julianobrasil avatar Oct 01 '18 16:10 julianobrasil