Llamar dayjs() da como resultado una fecha que es correcta, excepto que está desfasada por dos horas. Por alguna razón, dayjs() parece estar configurado en la zona horaria incorrecta (GMT), cuando mi zona horaria real es GMT+2.
Mon, 09 Aug 2021 17:45:55 GMT+2 Mon, 09 Aug 2021 15:45:55 GMTHe intentado configurar mi zona horaria con el complemento de zona horaria , pero parece que no funciona:
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 GMTEstoy en Ubuntu 20.04.2 LTS, así que verifiqué:
$ 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'. Estoy codificando en TypeScript, así que también verifiqué si la creación de un objeto de Date también daría como resultado un momento incorrecto, pero no fue así:
const time = new Date(); // results in correct time dayjs() está en GMT, pero debería estar en GMT+2. ¿Por qué?
Simplemente usar el complemento utc sin el complemento de zona horaria de alguna manera tuvo el efecto deseado.
import utc from 'dayjs/plugin/utc'; day.extend(utc); dayjs.utc(); // results in date in correct timezoneEsto es lo que funciona para mí.
dayjs('2021-08-09 15:45:55 UTC').tz("Africa/Lagos")Respuesta
{ '$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 }import utc from 'dayjs/plugin/utc'; import timezone from 'dayjs/plugin/timezone'; dayjs.extend(utc); dayjs.extend(timezone); dayjs.tz.setDefault('Europe/Berlin');
Deberías probar de esta manera. Sin embargo, tenga en cuenta que solo afecta a dayjs.tz('alguna fecha'), dayjs() todavía mostrará su hora local.