Rob*_*uer 7 javascript linux ubuntu typescript dayjs
调用dayjs()结果的日期是正确的,只是晚了两个小时。由于某种原因,dayjs()似乎设置了错误的时区(GMT),而我的实际时区是 GMT+2。
Mon, 09 Aug 2021 17:45:55 GMT+2
Run Code Online (Sandbox Code Playgroud)
Mon, 09 Aug 2021 15:45:55 GMT
Run Code Online (Sandbox Code Playgroud)
我尝试使用时区插件设置时区,但这似乎不起作用:
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
Run Code Online (Sandbox Code Playgroud)
我使用的是 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'.
Run Code Online (Sandbox Code Playgroud)
我正在使用 TypeScript 进行编码,因此我还检查了创建Date对象是否也会导致错误的时间,但事实并非如此:
const time = new Date(); // results in correct time
Run Code Online (Sandbox Code Playgroud)
dayjs()是 GMT,但应该是 GMT+2。为什么?
简单地使用 utc 插件而不使用时区插件就达到了预期的效果。
import utc from 'dayjs/plugin/utc';
day.extend(utc);
dayjs.utc(); // results in date in correct timezone
Run Code Online (Sandbox Code Playgroud)