使用默认时间选择器以角度进行时间选择器验证

问题描述 投票:0回答:1

这是基本上我想要的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 分钟加电流

javascript html angular typescript angular-reactive-forms
1个回答
0
投票

据我了解你想要

  1. 您想要验证时间
  • 您不需要验证时间选择器中的时间,因为它会 只允许用户输入有效时间
  1. 当用户单击时间选择器时,当前时间 + 15 默认分钟?

    为什么在构建表单时不将其设置为默认时间?

    TimeFormControl: [新日期(Date.now() + 15 * 60000)]

© www.soinside.com 2019 - 2024. All rights reserved.