如何以反应形式为 datetime-local 赋值

How to assign value to datetime-local in a Reactive Form

我无法为表单中的`datetime-local 元素赋值。

来自模板的代码:

打字稿文件中的代码:

let dateTime : Date = new Date();

this.testForm = this.formBuilder.group({    
  'dateTime': new FormControl(dateTime),
});

结果:

使用打字稿将日期和时间分配给 datetime-local 的正确方法是什么

datetime-local 需要特定格式的值 (yyyy-mm-ddThh:mm)

您可以尝试以下方法,

const dateTime = new Date();
this.testForm = this.formBuilder.group({    
  dateTime: new FormControl(dateTime.toISOString().substring(0, 16)),
});