Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the date in angular reactive forms using patchValue

I'm trying to set the value of the datepicker control with time. Not able to accomplish

Tried converting to desired format

app.component.html:

<input id="requestdate" type="date" class="form-control" formControlName="requestdate" 

app.component.ts:

ngOnInit() {

this.loginForm = this.formBuilder.group({ 
                    requestdate : ['']
                  })

let date = new Date()
date = date.toLocaleDateString().substring(0,10)

this.loginForm.get('requestdate').patchValue(date)


}

Not able to see the transformed date

like image 221
Shivaay Avatar asked Jul 25 '19 09:07

Shivaay


People also ask

How can I set my reactive form date input value?

For setting default date value to <input> tag with type date you should use HTML-property value of input-tag. Variant 2 (recommended): You can use an built-in function formatDate() in angular.

What does patchValue do in angular?

patchValue()linkPatches the value of the FormGroup . It accepts an object with control names as keys, and does its best to match the values to the correct controls in the group.

What is the difference between setValue and patchValue?

SetValue Vs PatchValue The difference is that with setValue we must include all the controls, while with the patchValue you can exclude some controls.

How do you find the date of a reactive form?

For a form input of type datetime-local , the way to convert theDate for display appears to be this: formatDate(theDate,'yyyy-MM-ddTHH:mm','en') .


1 Answers

You seem to have used the wrong syntax while reassigning the date variable. Since it was initialized as a Date, it won't accept a string in the format that you're supplying it in. You'll have to use the YYYY-MM-DD format.

Try this:

import { Component } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  loginForm: FormGroup;

  constructor(
    private formBuilder: FormBuilder
  ) { }

  ngOnInit() {
    this.loginForm = this.formBuilder.group({
      requestdate: ['']
    })
    this.loginForm.get('requestdate').patchValue(this.formatDate(new Date()));
  }

  private formatDate(date) {
    const d = new Date(date);
    let month = '' + (d.getMonth() + 1);
    let day = '' + d.getDate();
    const year = d.getFullYear();
    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;
    return [year, month, day].join('-');
  }
}

Don't forget to wrap the input field around a form tag with formGroup attribute set to loginForm:

<form [formGroup]="loginForm">
  <input
    id="requestdate" 
    type="date" 
    class="form-control" 
    formControlName="requestdate" />
</form>

Here's a Working Sample StackBlitz for your ref.

like image 107
SiddAjmera Avatar answered Oct 16 '22 12:10

SiddAjmera