Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

414
Visualizações
dayjs() is in the wrong timezone

Problem

Calling dayjs() results in a date that is correct except it is off by two hours. For some reason, dayjs() seems to be set to the wrong time zone (GMT), when my actual time zone is 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

I have tried setting my time zone with the time zone plugin, but that didn't seem to work:

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'.

I am coding in TypeScript, so I also checked if creating a Date object would result in the wrong time too, but it did not:

const time = new Date(); // results in correct time

TL;DR

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

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Simply using the utc plugin without the timezone plugin somehow had the desired effect.

import utc from 'dayjs/plugin/utc';
day.extend(utc);
dayjs.utc(); // results in date in correct timezone
about 4 years ago · Juan Pablo Isaza Relatório

0

This is what works for me.

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
}
about 4 years ago · Juan Pablo Isaza Relatório

0

import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.tz.setDefault('Europe/Berlin');

You should try this way. However, note, that it affects only dayjs.tz('some date'), dayjs() still will show your local time.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda