[英]json date format using angular javascript
我正在尝试在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>
您可以使用管道 ( |
):
引入Angular管道,这是一种编写可以在HTML中声明的显示值转换的方法。
管道将数据作为输入,并将其转换为所需的输出。
更改
<td>{{certificate.date}}</td>
至
<td>{{certificate.date | date:'yyyy-MM-dd'}}</td>
为您提供2种选择:
(new Date(certificate.date)).toLocaleDateString()
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.