I need to select two different date formats (YYYY-MM-DD and YYYY-MM) in two mat datepicker fields in one component view.
I'm using angular 6.0.3 and material 6.4.7.
I configured datepicker format to YYYY-MM-DD in app module using MAT_DATE_FORMATS and it works globally but I need to override this format to YYYY-MM in a few datepicker fields as I wrote above. Unfortunatelly I have no idea how I can achieve this.
Could You help me please?
Parts of my code:
import { Component, OnInit, ViewChild } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
@Component({
selector: 'app-invoice-list',
templateUrl: './invoice-list.component.html',
styleUrls: ['./invoice-list.component.css']
})
export class InvoiceListComponent {
filterForm: FormGroup;
@ViewChild('month_picker') month_picker;
constructor() {
this.filterForm = new FormGroup({
assigned_date_from: new FormControl(),
assigned_date_to: new FormControl(),
assigned_month: new FormControl(),
});
}
}
<div class="container" [formGroup]="filterForm">
<div class="container" fxLayout="row" fxLayout.xs="column" fxLayoutWrap fxLayoutGap="0.5%" fxLayoutAlign="start">
<div fxFlex="32%">
<mat-form-field>
<input matInput [matDatepicker]="date_from_picker" (focus)="date_from_picker.open()" formControlName="assigned_date_from" placeholder="Date from">
<mat-datepicker-toggle matSuffix [for]="date_from_picker"></mat-datepicker-toggle>
<mat-datepicker #date_from_picker></mat-datepicker>
</mat-form-field>
</div>
<div fxFlex="32%">
<mat-form-field>
<input matInput [matDatepicker]="date_to_picker" (focus)="date_to_picker.open()" formControlName="assigned_date_to" placeholder="Date to">
<mat-datepicker-toggle matSuffix [for]="date_to_picker"></mat-datepicker-toggle>
<mat-datepicker #date_to_picker></mat-datepicker>
</mat-form-field>
</div>
<!-- In below input I want month year format - YYYY-MM - so different than default format in above fields -->
<div fxFlex="32%">
<mat-form-field>
<input matInput [matDatepicker]="month_picker" (focus)="month_picker.open()" formControlName="assigned_month" placeholder="Month">
<mat-datepicker-toggle matSuffix [for]="month_picker"></mat-datepicker-toggle>
<mat-datepicker #month_picker></mat-datepicker>
</mat-form-field>
</div>
</div>
</div>