gpt4 book ai didi

javascript - 将特定格式的日期转换为 ISO 格式

转载 作者:行者123 更新时间:2023-11-28 14:28:58 24 4
gpt4 key购买 nike

在 Material UI 日期选择器中,它只能接受特定格式的日期。但是,我需要将它们转换为 iso 格式,以便对它们执行一些其他操作。这是日期的格式。2018-09-06.我想将上面的内容转换成iso格式。这是生成日期的代码。

let today = new Date();
let dd = today.getDate();
let mm = today.getMonth() + 1; // January is 0!
let yyyy = today.getFullYear();

if (dd < 10) {
dd = '0' + dd
}

if (mm < 10) {
mm = '0' + mm
}

today = yyyy + '-' + mm + '-' + dd; // need to convert this to iso format .

使用简单的 toISOString() 对我毫无帮助。

最佳答案

today.toISOString() 应该可以正常工作。但是 toISOString 方法需要 Date 对象而不是纯日期字符串。

const unformattedDate = new Date(yyyy + '-' + mm + '-' + dd)
const formattedDate = unformattedDate.toISOString()

关于javascript - 将特定格式的日期转换为 ISO 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52198022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com