2

问题

调用dayjs()会产生一个正确的日期,但它会延迟两个小时。出于某种原因,dayjs()当我的实际时区为 GMT+2 时,似乎设置为错误的时区 (GMT)。

预期的

Mon, 09 Aug 2021 17:45:55 GMT+2

实际的

Mon, 09 Aug 2021 15:45:55 GMT

我试过的

我尝试使用时区插件设置我的时区,但这似乎不起作用:

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 time

TL;博士

dayjs()是 GMT,但应该是 GMT+2。为什么?

4

2 回答 2

1

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

import utc from 'dayjs/plugin/utc';
day.extend(utc);
dayjs.utc(); // results in date in correct timezone
于 2021-08-26T16:19:48.170 回答
0

这对我有用。

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
}
于 2021-11-15T15:43:00.043 回答