Jerald
Jerald

Reputation: 23

Angular 6 date format change

I try to change my angular mat date picker format, so I used on change method in input the date value format successfully print in the console, but I can't pass to the value in reactive form controller, anyone help me what I did wrong. I want to look like this format: DD/MM/YY

My HTML Code:

             <mat-form-field>
                <input formControlName="dob" matInput [matDatepicker]="picker" 
                (ngModelChange)="changefunction($event)" placeholder="Date Of Birth">
                <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
                <mat-datepicker #picker></mat-datepicker>
              </mat-form-field>


        Component code: 


            this.addmember = new FormGroup({
              "PatientFamilyMember" : new FormArray ([
               "dob" : new FormControl('')
             ]);
          });

  changefunction(i) {
    const momentDate = new Date(i); // Replace event.value with your date value
    const formattedDate = _moment(momentDate).format("MM-DD-YYYY");
    this.formatdate = formattedDate
    console.log(this.formatdate);
  }

Date prints successfully in console log but how do I pass the date into the form controller?

Upvotes: 1

Views: 875

Answers (1)

Sabin Bogati
Sabin Bogati

Reputation: 741

You don't have to change the date format here on "ngModelChange"... Do this after submit button is clicked... Otherwise have you tried patchValue ?

this.yourFormName.get("dob").patchValue(this.formatdate);

Upvotes: 1

Related Questions