Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having some issues when use a timezone with zero offset with locale method #2248

Open
nkeyy0 opened this issue Feb 27, 2023 · 2 comments · May be fixed by #2532
Open

Having some issues when use a timezone with zero offset with locale method #2248

nkeyy0 opened this issue Feb 27, 2023 · 2 comments · May be fixed by #2532

Comments

@nkeyy0
Copy link

nkeyy0 commented Feb 27, 2023

I'm having some issues with parsing timezones with zero offsets.

Issue description

I have unexpected results when applying locale for the dayjs object in UTC and timezone with zero offsets. When I call the locale method after .tz() looks like the locale method is changed date-time according to my local time. But with timezones which is different from UTC, everything works fine.

Code

My plugins

import dayjs from 'dayjs';
import advancedFormat from 'dayjs/plugin/advancedFormat';
import calendar from 'dayjs/plugin/calendar';
import customParseFormat from 'dayjs/plugin/customParseFormat';
import localizedFormat from 'dayjs/plugin/localizedFormat';
import timezone from 'dayjs/plugin/timezone';
import utc from 'dayjs/plugin/utc';

dayjs.extend(advancedFormat);
dayjs.extend(customParseFormat);
dayjs.extend(utc);
dayjs.extend(localizedFormat);
dayjs.extend(timezone);
dayjs.extend(calendar);

I don't import locales here, but I import them dynamically after a user selects a specific locale.

Code with issues

dayjs.utc(date).tz(timeZone).locale(locale).format(localizedFormats[dateFormat])

The date argument in that case is a date string in ISO format. Timezone is the string, locale is the string with one of the dayjs locales. Everything works fine until I pass the timezone with zero offsets. I've tried to test this behavior and found that the issue is probably in the clone method

Logs

Date argument is 2022-02-27T11:05:51.439Z in ISO format

Code

      console.log('only timezone', dayjs.utc(date).tz(timeZone));
      console.log('timezone + locale', dayjs.utc(date).tz(timeZone).locale(locale));
      console.log('clone + timezone', dayjs.utc(date).tz(timeZone).clone());

Results

  1. With zero offset
    image
  2. With non-zero offset
    image
    image

Expected behavior

The date and time should be the same after locale method call

Information

  • Day.js Version: 1.11.7
  • OS: Windows 10 (64-bit)
  • Browser: Firefox 110.0 (64-bit)
  • Time zone: Europe/Warsaw [UTC + 01:00]
@ohsory1324
Copy link

+1

@iamkun Do you know this issue?

@dungle1811
Copy link

dungle1811 commented Dec 6, 2023

I've got the same problem when I passed in the timezone "UTC":

dayjs.tz("2023-12-06T07:43:14.674Z", "UTC").locale("en").format("LT") returns wrong value 12:43 AM
dayjs.tz("2023-12-06T07:43:14.674Z", "UTC").format("LT") returns the expected value 7:43 AM

@ohsory1324 ohsory1324 linked a pull request Dec 20, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants