'Convert a mat-datepicker into a String
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!
Solution 1:[1]
HTML
<form [formGroup]="myForm">
<mat-form-field>
<mat-label>Date d'expiration</mat-label>
<input matInput [matDatepicker]="picker" formControlName="expireDate" (dateChange)="changeDatePicker()">
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
</form>
TS
Assign value to expiredate in formgroup
changeDatePicker(): any {
this.myForm.value.expireDate = moment(this.myForm.value.expireDate).format('YYYY-MM-DD');
}
- For Multiple Date Pickers
HTML
<form [formGroup]="myForm">
<mat-form-field>
<mat-label>Date d'expiration</mat-label>
<input matInput [matDatepicker]="picker" formControlName="expireDate" (dateChange)="changeDatePicker()">
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
<mat-form-field>
<mat-label>Date d'expiration</mat-label>
<input matInput [matDatepicker]="picker1" formControlName="expireDate2" (dateChange)="changeDatePicker()">
<mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
<mat-datepicker #picker1></mat-datepicker>
</mat-form-field>
</form>
TS
myForm = this.fb.group({
expireDate: ['', Validators.required],
expireDate2: ['', Validators.required],
});
changeDatePicker(): any {
this.myForm.value.expireDate = (moment(this.myForm.value.expireDate).format('YYYY-MM-DD'));
this.myForm.value.expireDate2 = (moment(this.myForm.value.expireDate2).format('YYYY-MM-DD'));
}
Solution 2:[2]
You can format the moment date object like this example:
const currentDate = moment();
moment(currentDate).format('YYYY-MM-DD');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Amine Safi |