日志里的两个 01:30,为什么实际差了一小时?
日志里同样显示 01:30 的两条记录,UTC 时间却相差一小时。用 Intl.DateTimeFormat 复现,再给时间列补上偏移量。
日志表有两行都显示 01:30。展开原始记录,一条是 2024-11-03T05:30:00Z,另一条是 2024-11-03T06:30:00Z。明明隔了一个小时,为什么转成纽约时间就一样了?
把表格里负责显示时间的代码单独拎出来。它指定了 America/New_York,然后只取小时和分钟。这里也顺手用原始时间戳算一下两条记录隔了多久。
const clock = new Intl.DateTimeFormat('en-US', {
timeZone: 'America/New_York',
hour: '2-digit',
minute: '2-digit',
hourCycle: 'h23',
});
const first = new Date('2024-11-03T05:30:00Z');
const second = new Date('2024-11-03T06:30:00Z');
console.log(clock.format(first));
console.log(clock.format(second));
console.log(`elapsed: ${(second.getTime() - first.getTime()) / 60_000} min`);


纽约的 01:30 过了两次
2024 年 11 月 3 日,纽约的时钟走到 02:00 时拨回了 01:00。第一次 01:30 用的是 UTC−04:00,第二次用的是 UTC−05:00。原来的格式只显示时和分,连上日期也一样,因为两条记录在纽约都是 11 月 3 日。

给时间列补上偏移
在 Intl.DateTimeFormat 里加上 timeZoneName: 'shortOffset'。日志列要固定显示成「时:分 偏移」的顺序,可以用 formatToParts() 取这三项再拼起来。
const clock = new Intl.DateTimeFormat('en-US', {
timeZone: 'America/New_York',
hour: '2-digit',
minute: '2-digit',
hourCycle: 'h23',
timeZoneName: 'shortOffset',
});
for (const iso of ['2024-11-03T05:30:00Z', '2024-11-03T06:30:00Z']) {
const parts = Object.fromEntries(
clock.formatToParts(new Date(iso)).map(({ type, value }) => [type, value]),
);
console.log(`${parts.hour}:${parts.minute} ${parts.timeZoneName}`);
}
这个例子在 en-US 下会分别显示 01:30 GMT-4 和 01:30 GMT-5。表格排序直接用原始时间戳,算耗时也拿两个毫秒值相减。排查重复写入时看事件 ID,同一毫秒照样可以记下两条不同的事件。
如果接口只收到 2024-11-03 01:30 和 America/New_York,信息就少了一截。接收端得能拿到 UTC 时间或偏移。旧接口只能传本地时间时,就得明确约定回拨时选第一次还是第二次。
TypeScript中怎么正确import JS文件并配置tsconfig
2026-12-05
JS正则表达式实现主流快递单号自动识别与归属判断
2026-12-04
JavaScript数组方法实战:map与filter中的回调函数示例
2026-12-04








