You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript如何将ISO日期转为2021-12-05T00:00:00.000+00:00格式

解决方案

方法1:重置UTC时间分量+字符串替换

这是最简便的实现方式,代码如下:

const now = new Date();
// 直接将UTC时间的时、分、秒、毫秒全部置为0
now.setUTCHours(0, 0, 0, 0);
// 将ISO字符串默认后缀Z替换为你需要的+00:00即可
const targetDate = now.toISOString().replace('Z', '+00:00');

执行后输出格式完全符合你的要求,例如2021-12-05T00:00:00.000+00:00。

说明:toISOString()输出的后缀Z和+00:00语义完全等价,都代表UTC零时区,替换后不会改变时间本身的含义。

方法2:手动拼接分量(无字符串替换)

如果不想做字符串替换,也可以手动拼接UTC日期的各个分量,兼容性更强:

const now = new Date();
const year = now.getUTCFullYear();
const month = String(now.getUTCMonth() + 1).padStart(2, '0');
const day = String(now.getUTCDate()).padStart(2, '0');
const targetDate = `${year}-${month}-${day}T00:00:00.000+00:00`;

两种方法均基于UTC时间计算,不会受本地时区影响出现日期偏移问题。

内容的提问来源于stack exchange,提问作者Kenny Quach

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 14:24:02