mat datepicker timezone not correct

Solutions on MaxInterview for mat datepicker timezone not correct by the best coders in the world

showing results for - "mat datepicker timezone not correct"
Arden
22 Apr 2020
1import { Inject, Injectable, Optional } from '@angular/core';
2import { MAT_DATE_LOCALE } from '@angular/material';   
3import { MomentDateAdapter } from '@angular/material-moment-adapter';
4import { Moment } from 'moment';
5import * as moment from 'moment';
6
7@Injectable()
8export class MomentUtcDateAdapter extends MomentDateAdapter {
9
10  constructor(@Optional() @Inject(MAT_DATE_LOCALE) dateLocale: string) {
11    super(dateLocale);
12  }
13
14  createDate(year: number, month: number, date: number): Moment {
15    // Moment.js will create an invalid date if any of the components are out of bounds, but we
16    // explicitly check each case so we can throw more descriptive errors.
17    if (month < 0 || month > 11) {
18      throw Error(`Invalid month index "${month}". Month index has to be between 0 and 11.`);
19    }
20
21    if (date < 1) {
22      throw Error(`Invalid date "${date}". Date has to be greater than 0.`);
23    }
24
25    let result = moment.utc({ year, month, date }).locale(this.locale);
26
27    // If the result isn't valid, the date must have been out of bounds for this month.
28    if (!result.isValid()) {
29      throw Error(`Invalid date "${date}" for month with index "${month}".`);
30    }
31
32    return result;
33  }
34}
35
Kat
31 Jan 2021
1providers: [
2    ...
3    { provide: MAT_DATE_LOCALE, useValue: 'en-GB' },
4    { provide: MAT_DATE_FORMATS, useValue: MAT_MOMENT_DATE_FORMATS },
5    { provide: DateAdapter, useClass: MomentUtcDateAdapter },
6    ...
7],
8