首页 > 解决方案 > 使用 Angular javascript 的 json 日期格式

问题描述

我正在尝试在 html 表中显示 json 数据。当我运行我的文件时,日期在我的表中显示为“2018-11-21T00:00:00.000+0000”。但我只需要显示“2018-11-21”。我怎么做?你能帮我把它分开吗?

import { Certificate } from './certificate';

export const CERTIFICATES: Certificate[] = [
    { date: '2018-11-21T00:00:00.000+0000', ident: 'Fe', moy_certified: 0.297 },
    { date: '2018-11-22T00:00:00.000+0000', ident: 'Cu', moy_certified: 0.04 },
    { date: '2018-11-23T00:00:00.000+0000', ident: 'Mn', moy_certified: 0.0374 }, 
    { date: '2018-11-24T00:00:00.000+0000', ident: 'V', moy_certified: 0.019 },
    { date: '2018-11-25T00:00:00.000+0000', ident: 'Mn', moy_certified: 0.037 }
];
<ul class="cert-result">
    <li *ngFor="let certificate of certificates">
      <table>
        <tr>
          <th>Date</th>
          <th>Element</th>
          <th>Composition</th>
        </tr>
        <tr>
          <td>{{certificate.date}}</td>
          <td>{{certificate.ident}}</td>
          <td>{{certificate.moy_certifiee}}</td>
        </tr>
      </table>
    </li>
  </ul>

标签: javascriptangular

解决方案


您可以使用管道( |):

介绍 Angular 管道,一种编写显示值转换的方法,您可以在 HTML 中声明。

管道将数据作为输入并将其转换为所需的输出。

改变

<td>{{certificate.date}}</td>

<td>{{certificate.date | date:'yyyy-MM-dd'}}</td>

推荐阅读