1

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.

4
  • Welcome to SO. Provide the code what you tried with minimal-reproducible-example Commented Oct 30, 2019 at 15:02
  • I have tried moment(this.myDate).format('YYYY-MM-DD').toString(); Commented Oct 30, 2019 at 15:04
  • Is this what you mean? stackblitz.com/angular/aqegoygyadb Commented Oct 30, 2019 at 15:09
  • Hey Paul, yeah but I am not able to get it to show in the datepicker using angular binding. I have a date which is in DD-MM-YYYY format and I need to display it in the datepicker. If I have a date as "Wed Oct 30 2019 20:44:26 GMT+0530 (India Standard Time)", it gets displayed, not not 30-10-2019 Commented Oct 30, 2019 at 15:15

2 Answers 2

1

try this

this.date = new Date(this.myDate);
Sign up to request clarification or add additional context in comments.

Comments

0

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.

6 Comments

Hey Allabakash, thanks for your help. I did this.startDate = moment(data.startDate, 'DD-MM-YYYY').toISOString(); and it worked but its displaying the date in MM/DD/YYY format.
In the attached code link, there is config with name MY_FORMATS, just change format of dateInput inside display object to the format you wanted to see.
Sorry but it did not work, the format is correct, although I tried changing it but it did not work.
did you change MY_FORMATS ? In the code link stackblitz.com/edit/angular-w9szcc. Date format is showing as DD-MM-YYYY only . What format you are expecting ?
I am expecting DD-MM-YYYY. MY_FORMATS has DD-MM-YYYY so I dont think I have to change it. Here is what I have:
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.