1

I've tried to display date in mat-datepicker (angular-material) in UTC time using:

{provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS,
useValue: {useUtc: true}

but it won't work.

How may I do that?

1
  • Vladlimir, welcome to stackoverflow. Please be more specific on your question. How is the code you posted above used on your project, what is the html and ts code of the component where you're using mat-datepicker. When you say "it won't work" what do you mean by that? What is the expected behaviour and what is happening? Are you seeing any error? Commented Mar 29, 2019 at 19:03

1 Answer 1

1

Hi I had this problem too and have found an answer elsewhere so seems a good idea to add it here.

import { 
    MAT_MOMENT_DATE_FORMATS, MomentDateAdapter,
    MAT_MOMENT_DATE_ADAPTER_OPTIONS } from '@angular/material-moment-adapter'; 
import {
    DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE  } from '@angular/material/core'; 
import * as moment from 'moment';

providers: [
    {provide: MAT_DATE_LOCALE, useValue: 'en-GB'}, // optional but gets my dates in dd/mm/yy format
    {provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS, useValue: {useUtc: true}},
    {
      provide: DateAdapter,
      useClass: MomentDateAdapter,
      deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS]
    },
    {provide: MAT_DATE_FORMATS, useValue: MAT_MOMENT_DATE_FORMATS},   
],

and then dont use : Date types, use : moment.Moment types.

That should clear it up but you can also use moment.utc() to really double down on it.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.