javascript - dayjs() 的时区错误

标签 javascript linux typescript ubuntu dayjs

问题
调用dayjs()结果是一个正确的日期,除了它关闭了两个小时。出于某种原因,dayjs()当我的实际时区是 GMT+2 时,似乎设置为错误的时区 (GMT)。
预期的

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 object 也会导致错误的时间,但它没有:
const time = new Date(); // results in correct time
TL;博士dayjs()是 GMT,但应该是 GMT+2。为什么?

最佳答案

简单地使用没有时区插件的 utc 插件就可以达到预期的效果。

import utc from 'dayjs/plugin/utc';
day.extend(utc);
dayjs.utc(); // results in date in correct timezone

关于javascript - dayjs() 的时区错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68715540/

相关文章:

javascript - 页面加载后使用 css 和 jquery 加载器进行预加载

linux - 为什么我的计时器不是周期性的而是只过期一次?

typescript - 在字符串值成员中使用枚举

javascript - 以字符串形式获取完整的 DOM 堆栈

javascript - 如何处理固定日期的突变?

c - 如何将段错误地址映射到对应的C代码?

linux - Linux 中的进程 ID 耗尽

Angular 拆分列表

visual-studio - 如何在 Visual Studio 2015 中启用 TypeScript 文件的预览面板?

javascript - 如何向复制的网页文本添加额外信息