Last active
December 16, 2023 03:27
-
-
Save predorock/6a60955b99bdf8e2077a49aadf039ecb to your computer and use it in GitHub Desktop.
Firestore Timestamp to Angular Material Datepicker date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Directive, HostListener } from '@angular/core'; | |
import { NgModel } from '@angular/forms'; | |
import * as firebase from 'firebase'; | |
@Directive({ | |
selector: '[appFirestoreDatepickerAdapter]', | |
providers: [NgModel], | |
}) | |
export class FirestoreDatepickerAdapterDirective { | |
constructor( | |
private model: NgModel | |
) { } | |
@HostListener('ngModelChange', ['$event']) | |
parse(ev): void { | |
if (ev instanceof firebase.firestore.Timestamp) { | |
this.model.valueAccessor.writeValue(ev.toDate()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<mat-form-field appearance="fill"> | |
<mat-label>Date</mat-label> | |
<input appFirestoreDatepickerAdapter matInput [matDatepicker]="myDateDatepicker" formControlName="myDate" id="myDate"> | |
<mat-datepicker-toggle matSuffix [for]="myDateDatepicker"></mat-datepicker-toggle> | |
<mat-datepicker #myDateDatepicker ></mat-datepicker> | |
</mat-form-field> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment