【发布时间】:2021-08-01 15:39:04
【问题描述】:
我正在尝试使用 Nodejs 中的 date-fns 将 utc 时间解析为时区
const date = new Date('2018-09-01T16:01:36.386Z');
const timeZone = 'Europe/Berlin';
const zonedDate = utcToZonedTime(date, timeZone);
// zonedDate could be used to initialize a date picker or display the formatted local date/time
// Set the output to "1.9.2018 18:01:36.386 GMT+02:00 (CEST)"
const pattern = "d.M.yyyy HH:mm:ss.SSS 'GMT' XXX (z)";
const output = format(zonedDate, pattern, { timeZone: 'Europe/Berlin' });
console.log(output);
我正在使用文档中的此代码来检查我的问题,但它并没有像文档所说的那样打印。 这是输出:
1.9.2018 18:01:36.386 GMT Z (GMT+0)
我不知道为什么时间部分会改变,但时区仍然是 UTC。应该是:
1.9.2018 18:01:36.386 GMT+02:00 (CEST)
注意:我只是使用这个示例代码来显示我的问题
节点 14.16.0
日期-fns:2.21.3
日期-fns-tz:1.1.4
【问题讨论】: