question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Day incorrect in datepicker

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:23
  • Comments:46 (19 by maintainers)

github_iconTop GitHub Comments

47reactions
ankitraonkacommented, Nov 13, 2017

@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

31reactions
Silthuscommented, Apr 30, 2018

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);
Read more comments on GitHub >

github_iconTop Results From Across the Web

day incorrect in angular material datepicker - Stack Overflow
If the hour of the date is lower than 12, it means the date is the day tomorrow and you need to correct...
Read more >
jquery ui datepicker showing wrong date — DataTables forums
I want to use the jquery UI datepicker in this case as it is more customizable.(I only want the user to select month/year...
Read more >
Datepicker form values display wrong timezone using ... - Reddit
Datepicker form values display wrong timezone using MomentDateAdapter. I can't seem to get the date picker to set a date in my local...
Read more >
day incorrect in angular material datepicker - iTecNote
i am using angular reactive form and MatMomentDateModule for date picker . the problem is related to timezones but i just want to...
Read more >
Datepicker in Angular using mat-datepicker | Material Design
To implement date picker in Angular we can use angular material datepicker ... For example to display date along with day use format...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found