JavaScript - 将日期转换为 ISOString(),毫秒四舍五入为两位数
JavaScript - Convert date to ISOString() with milliseconds rounded to two digit
我正在尝试设置一个具有 ISOString 格式的日期,但毫秒应设置为两位数。
如果ISOString returns 2021-11-02T05:49:12.704Z 我希望它是2021-11-02T05:49:12.70Z(毫秒舍入到两位)
这就是我想要做的。
let startdate = new Date();
console.log(startdate.getMilliseconds()); //just to check
let d = startdate.toString().replace(microsecond=Math.round(startdate.microsecond, 2))
console.log(d) //format is different
startdate = startdate.toISOString() //this is different than d
console.log(startdate)
这个的输出是
961
Tue Nov 02 2021 05:50:46 GMT+0000 (Coordinated Universal Time)
2021-11-02T05:50:46.961Z
有人能帮忙吗?
如果你喜欢 t运行 cating,而不是四舍五入,这很有趣,虽然有点神秘:
> d = new Date().toISOString()
'2021-11-02T06:00:01.601Z'
> d.replace(/(\d\d\.\d\d)\d/, "",)
'2021-11-02T06:00:01.60Z'
它依赖于 toISOString
始终在小数点后生成 3 位数字。这里可以使用replace
只保留前两位。
这是另一个 运行:
> d = new Date().toISOString()
'2021-11-02T06:02:25.420Z'
> d.replace(/(\d\d\.\d\d)\d/, "",)
'2021-11-02T06:02:25.42Z'
正如我提到的,它 t运行cate 而不是圆形,因此您可能会注意到以下情况:
> d = new Date().toISOString()
'2021-11-02T06:03:53.157Z'
> d.replace(/(\d\d\.\d\d)\d/, "",)
'2021-11-02T06:03:53.15Z'
现在,如果您真的想要四舍五入,试试这个:
> d.replace(/(\d\d\.\d\d\d)/, s=>Number(s).toFixed(2))
'2021-11-02T06:03:53.16Z'
那个人的优势在于可以根据实际数字进行计算。但是它会在 0 到 9 之间的几秒内失败,所以借用 Phil 的正确答案,使用 padStart
确保在几秒内保持前导零:
> d = '2021-11-02T06:22:03.266Z'
> d.replace(/(\d\d\.\d\d\d)/, s=>Number(s).toFixed(2).padStart(5, '0'))
'2021-11-02T06:22:03.27Z'
菲尔的正则表达式也更通用,所以接受这个答案。
舍入毫秒部分并替换尾随 0Z
new Date(Math.round(new Date().getTime() / 10) * 10).toISOString().replace('0Z', 'Z')
如果是简单截断,使用Math.floor
或正则表达式替换
new Date(Math.floor(new Date().getTime() / 10) * 10).toISOString().replace('0Z', 'Z')
new Date().toISOString().replace(/\dZ$/, 'Z')
解析出秒和毫秒并替换为固定精度的数字
const now = new Date()
const formatted = now
.toISOString()
.replace(/\d{2}\.\d{3,}(?=Z$)/, num =>
Number(num).toFixed(2).padStart(5, "0"))
console.log(formatted)
我正在尝试设置一个具有 ISOString 格式的日期,但毫秒应设置为两位数。
如果ISOString returns 2021-11-02T05:49:12.704Z 我希望它是2021-11-02T05:49:12.70Z(毫秒舍入到两位)
这就是我想要做的。
let startdate = new Date();
console.log(startdate.getMilliseconds()); //just to check
let d = startdate.toString().replace(microsecond=Math.round(startdate.microsecond, 2))
console.log(d) //format is different
startdate = startdate.toISOString() //this is different than d
console.log(startdate)
这个的输出是
961
Tue Nov 02 2021 05:50:46 GMT+0000 (Coordinated Universal Time)
2021-11-02T05:50:46.961Z
有人能帮忙吗?
如果你喜欢 t运行 cating,而不是四舍五入,这很有趣,虽然有点神秘:
> d = new Date().toISOString()
'2021-11-02T06:00:01.601Z'
> d.replace(/(\d\d\.\d\d)\d/, "",)
'2021-11-02T06:00:01.60Z'
它依赖于 toISOString
始终在小数点后生成 3 位数字。这里可以使用replace
只保留前两位。
这是另一个 运行:
> d = new Date().toISOString()
'2021-11-02T06:02:25.420Z'
> d.replace(/(\d\d\.\d\d)\d/, "",)
'2021-11-02T06:02:25.42Z'
正如我提到的,它 t运行cate 而不是圆形,因此您可能会注意到以下情况:
> d = new Date().toISOString()
'2021-11-02T06:03:53.157Z'
> d.replace(/(\d\d\.\d\d)\d/, "",)
'2021-11-02T06:03:53.15Z'
现在,如果您真的想要四舍五入,试试这个:
> d.replace(/(\d\d\.\d\d\d)/, s=>Number(s).toFixed(2))
'2021-11-02T06:03:53.16Z'
那个人的优势在于可以根据实际数字进行计算。但是它会在 0 到 9 之间的几秒内失败,所以借用 Phil 的正确答案,使用 padStart
确保在几秒内保持前导零:
> d = '2021-11-02T06:22:03.266Z'
> d.replace(/(\d\d\.\d\d\d)/, s=>Number(s).toFixed(2).padStart(5, '0'))
'2021-11-02T06:22:03.27Z'
菲尔的正则表达式也更通用,所以接受这个答案。
舍入毫秒部分并替换尾随 0Z
new Date(Math.round(new Date().getTime() / 10) * 10).toISOString().replace('0Z', 'Z')
如果是简单截断,使用Math.floor
或正则表达式替换
new Date(Math.floor(new Date().getTime() / 10) * 10).toISOString().replace('0Z', 'Z')
new Date().toISOString().replace(/\dZ$/, 'Z')
解析出秒和毫秒并替换为固定精度的数字
const now = new Date()
const formatted = now
.toISOString()
.replace(/\d{2}\.\d{3,}(?=Z$)/, num =>
Number(num).toFixed(2).padStart(5, "0"))
console.log(formatted)