问题
调用dayjs()的结果是一个正确的日期,只是差了两个小时。由于某些原因,当我的实际时区是GMT+2时,dayjs()似乎被设置为错误的时区。
期望的
Mon, 09 Aug 2021 17:45:55 GMT+2实际
Mon, 09 Aug 2021 15:45:55 GMT我尝试过的
我试着用the time zone plugin设置我的时区,但似乎不起作用:
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs().tz('Europe/Berlin'); // unchanged Mon, 09 Aug 2021 15:45:55 GMT我使用的是Ubuntu 20.04.2 LTS,所以我检查了:
$ timedatectl
Local time: Mo 2021-08-09 17:45:55 CEST
Universal time: Mo 2021-08-09 15:45:55 UTC
RTC time: Mo 2021-08-09 17:45:55
Time zone: Europe/Berlin (CEST, +0200)
System clock synchronized: yes
NTP service: active
RTC in local TZ: yes
Warning: The system is configured to read the RTC time in the local time zone.
This mode cannot be fully supported. It will create various problems
with time zone changes and daylight saving time adjustments. The RTC
time is never updated, it relies on external facilities to maintain it.
If at all possible, use RTC in UTC by calling
'timedatectl set-local-rtc 0'.我是用TypeScript编写代码的,所以我还检查了创建Date对象是否也会导致错误的时间,但它没有:
const time = new Date(); // results in correct timeTL;DR
dayjs()在格林尼治标准时间,但应该在GMT+2中。为什么?
发布于 2021-08-26 16:19:48
简单地使用utc插件而不使用时区插件在某种程度上达到了预期的效果。
import utc from 'dayjs/plugin/utc';
day.extend(utc);
dayjs.utc(); // results in date in correct timezone发布于 2021-11-15 15:43:00
这就是对我有效的方法。
dayjs('2021-08-09 15:45:55 UTC').tz("Africa/Lagos")响应
{
'$L': 'en',
'$offset': 60,
'$d': 2021-08-09T15:45:55.000Z,
'$x': { '$timezone': 'Africa/Lagos' },
'$y': 2021,
'$M': 7,
'$D': 9,
'$W': 1,
'$H': 16,
'$m': 45,
'$s': 55,
'$ms': 0
}https://stackoverflow.com/questions/68715540
复制相似问题