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

Maybe the problem here is the fact that the datepicker's value is a Date in the first place. Date is a perfectly good(ish) class for representing an absolute instant in real time; it's a terrible representation of a calendar date. This entire thread, dozens of posts, dozens of users complaining, could have been avoided if the default date adapter went to/from ISO 8601 date-only strings. Every date library (and of course the native Date object) understands them, they are exactly as specific as the UI that generated them, and they can be "upgraded" to an instant or range of real time by simply adding "Z" on the end. What I mean to say is, adding context to go from "2018/11/26" to "2018/11/26Z" makes sense, but going the other way -- trying to remove time-zone-ness from the data -- does not.

Of course, I can fix all this by writing my own date adapter, or going out and getting the Moment-based one, but as long as the default behavior is Date-based, you're going to keep seeing people showing up in this thread.

thw0rted avatar Nov 26 '18 12:11 thw0rted

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);

thanks, it's work very well :)

nugrohotanjo avatar Apr 20 '19 04:04 nugrohotanjo

@Silthus the adapter you did works like a charm, but I have a requirement to set the format for the date. How can I achieve that alongside timezone adapter?

my date format: "DD MMM YYYY" // 15 jul 2019

Achyuthkodali avatar Jul 15 '19 12:07 Achyuthkodali

I am wondering if adding the useUTC portion to a component's providers instead of the module's providers will not work?

When using the Honolulu timezone (spoofing the browser), we were not able to make moment work, we ended up having to manually remove the timezone offset in order to get the mat-datepicker to choose the correct date:

let newDate= new Date(initialInfo.value); newDate.setMinutes(newDate.getMinutes() + newDate.getTimezoneOffset()); variableValueCtrl.reset(newDate);

When I tried to get it to work with moment it cause a whole host of other issues. However this still causes the selection to be wrong:

Screen Shot 2019-09-09 at 2 10 57 PM

Not looking for a solution, just alerting to the issues

christrude avatar Sep 09 '19 18:09 christrude

Stackblitz showing crazy go nuts wonky-ness when you have your browser spoofed to India time:

https://stackblitz.com/edit/angular-trude-datetest

Screen Shot 2019-09-06 at 5 32 37 PM

And in that stack blitz, look at what it selects in the UI:

Screen Shot 2019-09-09 at 2 42 41 PM

So set to the 6th, moment converts to the 5th, mat-datepicker displays the 4th, but selects the 3rd.

christrude avatar Sep 09 '19 18:09 christrude

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.