【问题标题】:How to Truncate the fetched Date from the DynamoDB in react如何在反应中截断从 DynamoDB 中获取的日期
【发布时间】:2023-10-04 11:27:01
【问题描述】:

我从 DynamoDB 中获取数据,在这种情况下,数据是日期,所以它还带有时间和所有内容,

获取数据的代码,

<p>{this.sate.createdDate}</p>

输出:

2021-02-23T02:19:36.670Z

但我只需要日期应该是这样的:

预期输出:

2021-02-23

我尝试使用 substring 方法,但我没有明白, 对此有什么想法吗,请提一下,我会试试的,

感谢您的宝贵时间,

【问题讨论】:

    标签: javascript reactjs amazon-dynamodb substring truncate


    【解决方案1】:

    使用字符串 split() 方法

    const date = "2021-02-23T02:19:36.670Z"
    const date_in_desired_format = time.split("T")[0]; 
    
    console.log(date_in_desired_format); // 2021-02-23
    

    附加提示:如果您想以相同格式“ISO 格式”获取今天的日期

    使用这个:

    cosnt today_date_in_ISO_format = new Date().toISOString().split('T')[0]
    console.log(today_date_in_ISO_format); 
    

    【讨论】:

      最近更新 更多