4

I am using the angular material date picker . When I get the date I get the value as

Wed Sep 12 2018 00:00:00 GMT+0530 (India Standard Time)

but I need it to be in 'YYYY-MM-DD'

Here is my code so far

 <mat-form-field>
            <input matInput [matDatepicker]="picker" placeholder="Select a date" [(ngModel)] = 'dateSel' data-date-format='YYYY-MM-DD' >
            <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
            <mat-datepicker #picker></mat-datepicker>
        </mat-form-field>

I tried to use the moment.js application too. But I ended up with complicated date object.

2

1 Answer 1

6
 const momentDate = new Date(event.value); // Replace event.value with your date value
 const formattedDate = moment(momentDate).format("YYYY/MM/DD");
 console.log(formattedDate);
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Ashraful Islam - can you add the html as its giving invalid date format error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.