格式化日期时间从Angular中的数据库获取

时间:2019-04-02 10:29:40

标签: angular datetime

我有从数据库获取的datetime数据,原始数据datetime为2019-04-02T17:14:54.204515Z 在View Component中,我使用{{acc.updated_at|date:'dd/MM/yyyy HH:mm:ss'}},但收到的结果是03/04/2019 00:14:54。正确的结果是02/04/2019 17:14:54 我的代码可能有误吗?感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您可以通过以下方式实现:

 <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>

和.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();

答案 1 :(得分:-1)

您可以通过在HTML中使用Object.prototype.h = Object.prototype.hasOwnProperty; const data = {a:1}; if(data.h('a')){ console.log('success'); }else{ console.log('false'); }文件管理器来格式化日期

第一个解决方案

date

或者您可以将其格式化为ts文件

第二个解决方案

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

通过使用日期对象功能(获取月份等)

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

您的问题来自区域,您应该在24而非12系统中显示小时 请看这里你可以找到解决方案 How can I deal with the timezone issue with the Angular 4 date pipe?