账单日写着 5 月 1 日,页面却显示 4 月 30 日
同一账单在 UTC 显示 5 月 1 日,在洛杉矶却显示 4 月 30 日。用两组输出定位 Date 的解析行为,并分别处理到期日和付款时间。
账单接口给的是 dueDate: "2026-05-01"。按 UTC 展示,表格里写着 5 月 1 日。只改展示时区,切到洛杉矶,同一格就成了 4 月 30 日。要查这一天是在哪丢的,我会先把传给格式化器的值打出来。

第一行就够定位了。执行 new Date("2026-05-01") 时,JavaScript 把这种只含年月日的字符串解析成了 UTC 零点。这个时间点在洛杉矶是 4 月 30 日 17:00(PDT)。等到格式化函数接手,原来的“5 月 1 日”已经成了一个可以换算到任意时区的时间点。
function displayInstant(instant, timeZone) {
const parts = new Intl.DateTimeFormat("en-US", {
timeZone,
year: "numeric",
month: "2-digit",
day: "2-digit",
}).formatToParts(instant);
const { year, month, day } = Object.fromEntries(
parts.map(({ type, value }) => [type, value]),
);
return `${year}-${month}-${day}`;
}
const { dueDate } = { dueDate: "2026-05-01" };
const parsed = new Date(dueDate);
console.log("parsed:", parsed.toISOString());
console.log("UTC:", displayInstant(parsed, "UTC"));
console.log("Los Angeles:", displayInstant(parsed, "America/Los_Angeles"));
console.log("dueDate:", dueDate);

这里把时区作为参数传进去,方便拿同一个 Date 做对照,省得在系统设置里来回切换。用 formatToParts() 取出年月日,再拼成固定顺序,也方便直接看出哪一段变了。
账单上有两个日期,只给一个换时区
如果接口约定 dueDate 在各地都指 5 月 1 日,表格就直接拿校验过的 YYYY-MM-DD 展示。产品要显示成“2026 年 5 月 1 日”,拆开年月日就能排版。这样从接口到表格,dueDate 始终是个日历日期。
paidAt 记录的是付款发生的那一刻,应当走时间点的格式化函数。付款时间如果是 2026-05-01T00:00:00Z,洛杉矶看到 4 月 30 日是对的。同一张账单上,到期日和付款日因此可以显示成两天。

写回归测试时,用一条同时带有这两个字段的账单就够了。按 UTC 渲染时,两列都是 2026-05-01。按洛杉矶时区渲染时,到期日仍是 2026-05-01,付款日变成 2026-04-30。只测 UTC 的话,就算有人又把两列接进同一个时间点格式化函数,测试也会过。
JS split的limit参数怎么限制返回数组长度?
2026-12-05
JS数组操作性能对比:for循环、for...of与高阶函数怎么选择
2026-12-05
core-js 是什么:JavaScript 标准库的 Polyfill 集合
2026-12-04
上传还没开始,为什么草稿已经显示进度条?
2026-12-04
JS 给数值加一的三种写法:++、+=1 与 = + 1
2026-12-04








