I'm working with Angular Material and moment.js. I had the problem that dates were parsed according to my timezone, instead of just, well, what the user entered.
There's already a question about it here and it also has a working answer. But it's rather old, not taking into account standalone components, which do offer the possibility of providing stuff directly at the component level. However, that does not work in this case:
@Component({
selector: 'app-component-with-material-datepicker',
templateUrl: './component-with-material-datepicker.component.html',
styleUrls: ['./component-with-material-datepicker.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
imports: [
CommonModule,
FormsModule,
ReactiveFormsModule,
MatDatepickerModule,
MatMomentDateModule,
],
providers: [
{ provide: ErrorStateMatcher, useClass: InstantErrorStateMatcher }, // works
MomentDateAdapter,
{ provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS, useValue: { useUtc: true } }, // no effect
],
})
export class ComponentWithMaterialDatePicker {
// ...
This leaves me no choice but to provide the config at module level. I wish to understand why.
But that provider is an
environmentProviderand they need to be placed in the providers array ofbootstrapApplicationsecond parameter. So could you check the below stackblitz. Where I add in environment providers and it seems to work fine!Important points:
import
code
code
stackblitz