javascript - 使用 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>
解决方案
您可以使用管道( |
):
介绍 Angular 管道,一种编写显示值转换的方法,您可以在 HTML 中声明。
管道将数据作为输入并将其转换为所需的输出。
改变
<td>{{certificate.date}}</td>
至
<td>{{certificate.date | date:'yyyy-MM-dd'}}</td>
推荐阅读
- php - PHP preg_replace 逗号选择选项
- postgresql - 如何在 Hasura 中建模涉及多个字段的自定义关系?
- javascript - React/React Native:子组件状态的变化会重新渲染整个组件
- javascript - 如何在发布请求中上传带有 csrf 令牌的文件
- c# - app.ApplicationServices.GetService() 有什么作用?
- c# - Discord.NET 对象引用未设置为对象的实例
- entity-framework-6 - 在 EF6 中动态创建 DbConfiguration
- node.js - 是否可以仅获取 HTTP 标头而不使用 NodeJS 中的 Axios 下载内容正文?
- verilog - Verilog:if语句和case语句之间的区别
- java - JavaFX Canvas fillPolygon 方法不会创建平滑表面(网格线可见)