I'm trying to have a reactive form in which there is some dates that are entered by the user using mat-datepicker. The problem I face is that the output of the mat-datepicker is a moment object containing the date. I want to get the date in a String (for example : "YYYY-MM-DD") and not an object.
Currently, the html template of my form component looks like that :
<form [formGroup]="myForm">
<mat-form-field>
<mat-label>Date d'expiration</mat-label>
<input matInput [matDatepicker]="picker" formControlName="expireDate">
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
</form>
And, my component looks like that :
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms'
declare var require: any
const moment = require('moment');
@Component({
selector: 'app-form',
templateUrl: './form.component.html',
styleUrls: ['./form.component.css'],
})
export class FormComponent implements OnInit{
myForm : FormGroup;
constructor(private fb: FormBuilder) { }
ngOnInit(): void {
this.myForm = this.fb.group({
expireDate: [moment(),[
Validators.required,
]],
});
}
}
Thanks in advance!