Why is the date in datepicker angular showing the last day? Why is the date in datepicker angular showing the last day? angular angular

Why is the date in datepicker angular showing the last day?


I solve this problem by converting datepicker date value to UTC. Here is example:

const d = new Date(<DATE FROM FORM FIELD>);const date = Date.UTC(d.getFullYear(), d.getMonth(), d.getDate());

Maybe it's not elegant way to fix it but help me


I had a similar problem. I solved it by configuring Datepicker to use UTC dates. (It uses local time by default)Just add some providers on your component declaration :

import {MomentDateAdapter, MAT_MOMENT_DATE_ADAPTER_OPTIONS} from '@angular/material-moment-adapter';import {DateAdapter, MAT_DATE_LOCALE} from '@angular/material/core';@Component({  providers: [    {provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS]},    {provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS, useValue: {useUtc: true}}  ]})


Similar to the accepted answer, but a little less verbose and will keep the hours/minutes/seconds.

const dt = new Date(event.value);const tz = dt.getTimezoneOffset();const seconds = (dt.getTime() / 1000) - (tz * 60);