【问题标题】:Format ISO date in API response data在 API 响应数据中格式化 ISO 日期
【发布时间】:2021-03-21 16:04:31
【问题描述】:

如何在 {{item.Date}} 中格式化日期
我得到了很多行,但我不知道一次将它们全部格式化
我认为将 MySQL 中的数据类型设置为 Date 可以修复它,但显然没有。 提前致谢! 我得到了什么

2021-03-18T23:00:00.00Z

我想要什么

2021-03-15

我的代码

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>

【问题讨论】:

  • 循环遍历行和 slice() 字符串是一个相当简单的操作

标签: javascript node.js date format iso8601


【解决方案1】:

您有一个 ISO 8601 日期字符串,并且由于位置是固定的,您知道您只需要前 10 个字符。

最简单的解决方案是使用substring()

例子:

{{ item.Date.substring(0, 10) }}

【讨论】:

  • 谢谢,内森!你知道有什么方法可以从日期中删除前导零,例如 2021-03-21 到 2021-3-15 吗?我试过 .replace(/-0+/g, '-') ,它对我不起作用!
  • 没问题 :) 当您使用您发布的replace 时,您看到了什么?乍一看,它似乎应该像您期望的那样工作。 {{ item.Date.substring(0, 10).replace(/-0+/g, '-') }}
猜你喜欢
  • 2012-03-18
  • 1970-01-01
  • 2023-01-03
  • 1970-01-01
  • 2019-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-28
相关资源
最近更新 更多