我的日期选择器正在按照UTC日期运行。因此,当我在12:01AM
打开日期选择器时,默认选择(在当前日期)仍在前一个日期,如下图所示。
在这里,我将系统时间设置为Aug 2 12:10AM
。但是选择仍然位于Aug 1st
本身。
<mat-form-field appearance="fill">
<mat-label
>Date Of Birth
<span style="color: red;">*</span></mat-label
>
<input
readonly
[max]="today"
formControlName="dateOfBirth"
matInput
[matDatepicker]="pickerxx"
(click)="pickerxx.open()"
/>
<mat-datepicker-toggle
matSuffix
[for]="pickerxx"
></mat-datepicker-toggle>
<mat-datepicker #pickerxx></mat-datepicker>
<mat-error
*ngIf="dateOfBirth.invalid && dateOfBirth.errors.required">
{{ alertMessage.dobReq }}
</mat-error>
</mat-form-field>
这是我的ts代码:
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { FormGroup, FormControl, Validators, FormBuilder, FormArray } from '@angular/forms';
import { Router } from '@angular/router';
public addPeopleForm: FormGroup;
public today = new Date();
constructor(
private formBuilder: FormBuilder,
private router: Router,
private tService: TimezoneConversionService
) {}
get dateOfBirth() { return this.addPeopleForm.get('dateOfBirth'); }
ngOnInit(): void {
this.today = this.tService.convertToLocalTimeZone(new Date());
this.initAddUser();
}
initAddUser() {
this.addPeopleForm = this.formBuilder.group({
'dateOfBirth': new FormControl('', [Validators.required]),
});
}
如何使它在默认时区工作?