Problem

调用 dayjs() 的结果是一个正确的日期,但它偏离了两个小时。由于某些原因, dayjs() 似乎被设置为错误的时区(GMT),而我的实际时区是GMT+2。

Expected

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

Actual

Mon, 09 Aug 2021 15:45:55 GMT

What I've tried

我已经尝试用以下方法设置我的时区时区插件但这似乎并不奏效。

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

I'm on Ubuntu 20.04.2 LTS, so I checked:

$ 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;DR

dayjs() is in GMT, but should be in GMT+2. Why?

5 个评论
当你做new Date('Mon, 09 Aug 2021 17:45:55 GMT+2')new Date('Mon, 09 Aug 2021 15:45:55 GMT')时,产生的日期对象是什么?它们有什么不同?
它们的结果是一样的:1:Mon Aug 09 2021 17:45:55 GMT+0200 (Central European Summer Time)2:Mon Aug 09 2021 17:45:55 GMT+0200 (Central European Summer Time)
在你的问题中,你说它 "偏离了两个小时"。是偏离两小时还是相同的准确时间?你能不能把你的时间存储为格林威治标准时间?
我的问题是,dayjs似乎使用了错误的本地时间。我的问题是,如果我在不同的机器上运行同样的代码,就不再工作了,因为在那台机器上,本地时间是正确的。
所以你是说,如果一台机器在德国,另一台机器在英国,你创建了新的日期并存储了这些UTC/ISO字符串值,那么在对面的机器上使用这些字符串就会产生不同的时间?不是本地,是不同的UTC/ISO时间。本地时间可以是 "不同的",但如果他们产生了相同的iso/utc字符串,那么他们就是相同的,对吗?
javascript
linux
typescript
ubuntu
dayjs
Robert Schauer
Robert Schauer
发布于 2021-08-10
4 个回答
Robert Schauer
Robert Schauer
发布于 2022-11-03
已采纳
0 人赞同

简单地使用utc插件而不使用时区插件,不知不觉中就达到了预期效果。

import utc from 'dayjs/plugin/utc';
day.extend(utc);
dayjs.utc(); // results in date in correct timezone
    
Adeyemi Kayode
Adeyemi Kayode
发布于 2022-11-03
0 人赞同

这就是对我有用的东西。

dayjs('2021-08-09 15:45:55 UTC').tz("Africa/Lagos")

Response

'$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
Tigran Petrosyan
Tigran Petrosyan
发布于 2022-11-03
0 人赞同
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.tz.setDefault('Europe/Berlin');

你应该试试这个方法。但是,请注意,它只影响dayjs.tz('某个日期'),dayjs()仍然会显示你的本地时间。

Cam CHN
Cam CHN
发布于 2022-11-03
0 人赞同

你可以像这样创建一个服务

// Filename : dayjs.ts
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";
import "dayjs/locale/fr";
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.locale("fr");
dayjs.tz.setDefault("Europe/Paris")
const timezonedDayjs = (...args: any[]) => {
    return dayjs(...args).tz();
const timezonedUnix = (value: number) => {
    return dayjs.unix(value).tz();
timezonedDayjs.unix = timezonedUnix;
timezonedDayjs.duration = dayjs.duration;