Yogesh Mali
Yogesh Mali

Reputation: 223

Unable to set date in Angular material datepicker

I have a date which is fetched from the backed as 30-10-2019 (DD-MM-YYYY) format. I am trying to get this date to be displayed in angular material datepicker but its not displaying the date.

I tried moment and used moment (this.myDate).format('YYYY-MM-DD').toString(); What am I missing? Any other method (without using moment) is also fine.

Upvotes: 1

Views: 1489

Answers (2)

Shankarlal D
Shankarlal D

Reputation: 183

try this

this.date = new Date(this.myDate);

Upvotes: 1

Allabakash
Allabakash

Reputation: 2027

you need pass the date format to moment like below and assign that to your formControl or ngModel. Assuming your ngModel is date then code will be

this.date = moment(this.myDate, 'DD-MM-YYYY');

Here is the working example : https://stackblitz.com/edit/angular-w9szcc.

Hope this helps.

Upvotes: 0

Related Questions