Search code examples
angulartypescriptdatetimedate-format

Show date in specific format date from code?


I have an angular2 v4 app and I have to show a date in a specific format.

I have this in my component:

currentDate: Date = new Date();

And in my HTML I have this.

<mat-form-field>
  <input matInput [(ngModel)]="currentReport.fromDate" type="text"/>
</mat-form-field>

But In running time, the input look like this:

Sat Nov 17 2018 17:35:26 GMT-0600 (hora estándar central)

How to show the date in format dd/mm/yyyy hh:mm using ngModel?


Solution

  • You need to format the Date object to a string. One way to do that is with moment .format (https://momentjs.com/docs/#/displaying/)

    currentDate: Date = moment(new Date()).format('DD/MM/YYYY HH:mm')