在 API 响应数据中格式化 ISO 日期
Format ISO date in API response data
如何在 {{item.Date}} 中格式化日期
我有很多行,但我不知道一次格式化它们
我认为将 MySQL 中的数据类型设置为 Date 会修复它,但显然不会。
提前致谢!
我得到了什么
2021-03-18T23:00:00.00Z
我想要的
2021-03-15
MyCode
API
app.get('/schedetails/:schid', (_req, _res) => {
mysqlConnection.query('Select * from ScheDetails where Schedule_ID = ?',[_req.params.schid], (err, rows, _fields) => {
if (!err)
_res.send(rows);
else
console.log(err);
})
});
.ts 文件
getScheduleDetail(item){
this.http.get("http://localhost:3000" +"/schedetails/"+ item).subscribe(res=>{
console.log(res)
this.schedetaildata = res;
})
}
html
<div *ngFor="let item of schedetaildata">
<p>{{item.Weekday}}</p>
<p>{{item.Time_start}}</p>
<p>{{item.Time_finish}}</p>
<p>{{item.Date}}</p>
</div>
您有一个 ISO 8601 日期字符串,并且由于位置是固定的,您知道您只需要前 10 个字符。
最简单的解决方案是使用 substring()
。
示例:
{{ item.Date.substring(0, 10) }}
如何在 {{item.Date}} 中格式化日期
我有很多行,但我不知道一次格式化它们
我认为将 MySQL 中的数据类型设置为 Date 会修复它,但显然不会。
提前致谢!
我得到了什么
2021-03-18T23:00:00.00Z
我想要的
2021-03-15
MyCode
API
app.get('/schedetails/:schid', (_req, _res) => {
mysqlConnection.query('Select * from ScheDetails where Schedule_ID = ?',[_req.params.schid], (err, rows, _fields) => {
if (!err)
_res.send(rows);
else
console.log(err);
})
});
.ts 文件
getScheduleDetail(item){
this.http.get("http://localhost:3000" +"/schedetails/"+ item).subscribe(res=>{
console.log(res)
this.schedetaildata = res;
})
}
html
<div *ngFor="let item of schedetaildata">
<p>{{item.Weekday}}</p>
<p>{{item.Time_start}}</p>
<p>{{item.Time_finish}}</p>
<p>{{item.Date}}</p>
</div>
您有一个 ISO 8601 日期字符串,并且由于位置是固定的,您知道您只需要前 10 个字符。
最简单的解决方案是使用 substring()
。
示例:
{{ item.Date.substring(0, 10) }}