Search code examples
angularangular5angular-reactive-forms

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


I am unable to assign value to `datetime-local element in a form.

Code from template:

Code from HTML template

Code from typescript file:

let dateTime : Date = new Date();

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

Result:

enter image description here

What is the proper way to assign date & time to datetime-local using typescript


Solution

  • datetime-local requires value in specific format (yyyy-mm-ddThh:mm)

    You can try the following,

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