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

Calling tz() overrides locale #2398

Closed
wang-yikai opened this issue Aug 2, 2023 · 4 comments · Fixed by #2420
Closed

Calling tz() overrides locale #2398

wang-yikai opened this issue Aug 2, 2023 · 4 comments · Fixed by #2420
Labels

Comments

@wang-yikai
Copy link

Running
dayjs('2020-01-08T15:14:59.741774+00:00') .locale('es') .tz('America/New_York') .format('LLL')
gives January 8, 2020 10:14 AM instead of 8 de enero de 2020 10:14.

However, switching the order of locale and tz, e.g.
dayjs('2020-01-08T15:14:59.741774+00:00') .tz('America/New_York') .locale('es') .format('LLL')
gives the correct result of 8 de enero de 2020 10:14.

I believe this is the result of the following line of code, which overrides the current locale:

const target = date.toLocaleString('en-US', { timeZone: timezone })

This is the intended behavior or is it something to be fixed to support the provided locale?

@ohsory1324
Copy link
Contributor

I think this issue(#2248) should be checked.

@timsand
Copy link
Contributor

timsand commented Aug 21, 2023

I'm encountering this issue as well. For me, it seems that the locale weekStart property is ignored if it also has a timezone.

Example:

const dayjs = require('dayjs');
const utc = require('dayjs/plugin/utc');
const timezone = require('dayjs/plugin/timezone');

dayjs.extend(utc);
dayjs.extend(timezone);

const now = '08/22/2023';

const withLocality = dayjs(now).locale({
  name: 'locale_only',
  weekStart: 3,
});
const withLocalityAndTimezone = dayjs(now).tz('America/New_York').locale({
  name: 'locale_and_tz',
  weekStart: 3,
});
const timezoneOnly = dayjs(now).tz('America/New_York');

const a = withLocality.startOf('w'); // returns the expected value of 2023-08-16
const b = withLocalityAndTimezone.startOf('w'); // returns the unexpected value of 2023-08-20
const c = timezoneOnly.startOf('w'); // returns the expected value of 2023-08-20

I believe that the line described above with target is actually OK - when that target is passed to d though it loses the locality context.

I created a PR here with what I think may help fix this (unless this is intended behavior).

@xg4
Copy link

xg4 commented Aug 23, 2023

let ins = d(target).$set(MS, this.$ms)

The .tz method creates a new dayjs object but does not inherit previous properties, causing the language setting to be lost.

@github-actions
Copy link

🎉 This issue has been resolved in version 1.11.10 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants