-2

I have datetime data get from my db, raw data datetime is 2019-04-02T17:14:54.204515Z In view Component, I using {{acc.updated_at|date:'dd/MM/yyyy HH:mm:ss'}} but my result I received is 03/04/2019 00:14:54. Correct result is 02/04/2019 17:14:54 May I have wrong in my code? Thanks for help!

Virtus
  • 67
  • 1
  • 2
  • 6
  • 1
    Possible duplicate of [Format date as dd/MM/yyyy using pipes](https://stackoverflow.com/questions/35754586/format-date-as-dd-mm-yyyy-using-pipes) – Harun Yilmaz Apr 02 '19 at 10:33
  • sorry, I already updated my solution! I saw it but not work to me – Virtus Apr 02 '19 at 10:40

2 Answers2

0

you can do this by:

 <table width="100%" border="1">
        <tr>
          <th colspan="2">Test format Date Angular</th>
        </tr>
        <tr>
          <th>Date</th>
          <th>Format</th>
        </tr>
        <tr>
          <th colspan="2">Model Date ({{dateNow}})</th>
        </tr>
        <tr *ngFor="let format of formatsDateTest">
          <td>{{dateNow | date: format}}</td>
          <td>{{format}}</td>
        </tr>
        <tr>
          <th colspan="2">Model string ISO ({{dateNowISO}})</th>
        </tr>
        <tr *ngFor="let format of formatsDateTest">
          <td>{{dateNowISO | date: format}}</td>
          <td>{{format}}</td>
        </tr>
        <tr>
          <th colspan="2">Model Number Milliseconds ({{dateNowMilliseconds}})</th>
        </tr>
        <tr *ngFor="let format of formatsDateTest">
          <td>{{dateNowMilliseconds | date: format}}</td>
          <td>{{format}}</td>
        </tr>
      </table>

and in .ts

 formatsDateTest: string[] = [
    'dd/MM/yyyy',
    'dd/MM/yyyy hh:mm:ss',
    'dd-MM-yyyy',
    'dd-MM-yyyy HH:mm:ss',
    'MM/dd/yyyy',
    'MM/dd/yyyy hh:mm:ss',
    'yyyy/MM/dd',
    'yyyy/MM/dd HH:mm:ss',
    'dd/MM/yy',
    'dd/MM/yy hh:mm:ss',
    ];

  dateNow : Date = new Date();
  dateNowISO = this.dateNow.toISOString();
  dateNowMilliseconds = this.dateNow.getTime();
Muhammad Ali
  • 369
  • 7
  • 23
-1

you can format date by using date filer in html like this

First solution

 <span>{{created |date:'dd.MM.yyyy, hh:mm'}}</span>

Or you can format it in ts file

Second solution

toDate = new Date();
let toDate=(this.toDate.getDate()+1)+"/"+this.toDate.getMonth()+"/"+this.toDate.getFullYear();

by use Date object function (get Month ... etc)

Update

Your issue is from zone and you should display hour in 24 not 12 system plz see here you can find the solution How can I deal with the timezone issue with the Angular 4 date pipe?

Kenana Reda
  • 430
  • 1
  • 9
  • 24