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

JS时间戳转Europe/London时区出现小时偏差问题咨询

JavaScript时间戳转日期时区小时显示异常问题解决

问题重现

执行以下代码时,伦敦时区的输出结果比实际GMT时间快1小时:

var timestamp = 1548374400000 //In milliseconds.
var time_string =  new Date(timestamp).toLocaleString("en-GB",{timezone:"Europe/London"})
console.log(time_string)

输出:25/01/2019, 01:00:00,但该时间戳对应伦敦时区(GMT)的正确时间应为2019年1月25日00:00:00。

问题原因

这是由于原生Date对象的时区处理依赖运行环境(浏览器/Node.js)的底层时区数据库,部分环境的Europe/London时区数据存在偏差,错误在非夏令时段(1月属于GMT,夏令时为3-10月)套用了BST(英国夏令时)的+1小时偏移。

解决方案

方案1:直接指定GMT时区

替换时区参数为GMT,确保输出标准格林尼治时间:

var timestamp = 1548374400000;
var time_string = new Date(timestamp).toLocaleString("en-GB", {timezone: "GMT"});
console.log(time_string); // 输出:25/01/2019, 00:00:00

方案2:使用第三方时区库

依赖更精准的时区数据,推荐使用luxon或date-fns-tz:
以luxon为例:

const { DateTime } = require('luxon');
var timestamp = 1548374400000;
const dt = DateTime.fromMillis(timestamp).setZone('Europe/London');
console.log(dt.toFormat('dd/MM/yyyy, HH:mm:ss')); // 输出:25/01/2019, 00:00:00

补充说明

原生Date的时区实现受限于环境的时区数据更新频率,第三方库会持续维护权威时区数据(如IANA时区数据库),能有效避免这类边界场景的错误。

内容的提问来源于stack exchange,提问作者F.I.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 17:36:03