这是基本上我想要的html代码,当我打开时间选择器切换时,默认情况下是当前时间,但我想要当前时间+15分钟。 onchange 方法我正在验证时间,但我也给用户带来了错误。但我们的要求是当用户点击切换时我需要在弹出窗口中显示 15 分钟 `
<mat-label>Time</mat-label>
<input type="time" matInput [formControl]="TimeFormControl" (change)="timeValidator(15)" >
`
timeValidator(maxTime) {
const currentDate = new Date();
const time = this.TimePicker.value.match(/(\d+)(:(\d\d))?\s*(p?)/);
enter const updatedCurrentDate = new Date(currentDate.getTime() + maxTime * 60 * 1000);
if (time) {
if (this.selectedDate.setHours(0, 0, 0, 0) === currentDate.setHours(0, 0, 0, 0)) {
this.selectedDateTime = new Date();
this.selectedDateTime.setHours(Number(time[1]) + (Number(time[1]) < 12 && time[4] ? 12 : 0));
this.selectedDateTime.setMinutes(Number(time[3]) || 0);
if (maxTime) {
const diff = (Number(updatedCurrentDate) - Number(this.selectedDateTime)) / (1000 * 60);
this.checkTime = diff > 1 ? true : false;
}
if (!this.checkTime) {
this.TimePicker.setErrors(null);
} else {
this.TimePicker.setErrors({
incorrect: true,
});
}
} else {
this.TimePicker.setErrors(null);
}
}
}
据我了解你想要
当用户单击时间选择器时,当前时间 + 15 默认分钟?
为什么在构建表单时不将其设置为默认时间?
TimeFormControl: [新日期(Date.now() + 15 * 60000)]