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

z timezone formatting option does not transform date #2770

Open
SirSerje opened this issue Nov 18, 2024 · 1 comment
Open

z timezone formatting option does not transform date #2770

SirSerje opened this issue Nov 18, 2024 · 1 comment

Comments

@SirSerje
Copy link

Describe the bug
When I want to use z as format value, it returns z without transformation

dayjs.extend(utc)
dayjs.extend(timezone)
// ...
export const formatDateTime = (
    datetime: string,
    timezoneStr: string,
    formatStr: string
  ): string => {
    const date = dayjs(datetime).tz(timezoneStr);
    return ${date.format(formatStr)} ${date.format('z')};
  };

test case:

beforeAll(() => {
  dayjs.extend(utc)
  dayjs.extend(timezone)
})
it('formats with the correct timezone abbreviation', () => {
    const datetime = '2024-11-18T12:00:00Z';
    const timezoneStr = 'Europe/London';
    const formatStr = 'YYYY-MM-DD HH:mm:ss';
    const formattedDate = formatDateTime(datetime, timezoneStr, formatStr);
    expect(formattedDate).toBe('2024-11-18 12:00:00 GMT'); // assuming GMT for the timezone
});

I've been looking into sources: https://github.com/iamkun/dayjs/blob/dev/src/index.js#L333 but unfortunaltelly lowercase z is not included

Expected behavior
z will be transformed correctly according to https://day.js.org/docs/en/plugin/advanced-format

Information

  • Day.js Version 1.11.13
  • OS: 14.6.1 (23G93)
  • Browser Version 130.0.6723.117 (Official Build) (arm64)
  • Time zone: Eastern Standard Time > Time zone in Ottawa, ON (GMT-5)
@ChaiyoKung
Copy link

ChaiyoKung commented Nov 19, 2024

Try to use advancedFormat plugin

import advancedFormat from "dayjs/plugin/advancedFormat";

dayjs.extend(advancedFormat);

Normal format function not support z, but you can use advancedFormat plugin to make it support z.

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

No branches or pull requests

2 participants