Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Weird behavior of FormattedDateTimeRange in React Native #4535

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

Open
JustJoostNL opened this issue Sep 21, 2024 · 11 comments
Open

Weird behavior of FormattedDateTimeRange in React Native #4535

JustJoostNL opened this issue Sep 21, 2024 · 11 comments

Comments

@JustJoostNL
Copy link

Which package?

react-intl

Describe the bug

I am using FormattedDateTimeRange in a React Native application with the following setup:

<FormattedDateTimeRange
  from={startDate}
  to={endDate}
  weekday="short"
  day="numeric"
  month="short"
  hour="2-digit"
  minute="2-digit"
  hour12={config.use12HourTime}
  year={thisYear ? undefined : "numeric"}
/>

When the from and to dates fall on the same day (with different times), I expect the output to be:

Sun, 22 Sept, 14:00–16:00

However, the actual output I see is:

Sun, 22 Sept, 12:00–Sun, 22 Sept, 14:00

There are two issues here:

  1. The date and day are duplicated.
  2. The times are shown in UTC instead of the user's device timezone.

When I remove the polyfills and use FormattedDate for testing, it correctly respects the device timezone, which makes me suspect this is an issue with the polyfills. Both of these issue don't occur in a normal browser environment (react, not react-native).

To Reproduce

Add the following polyfills:

import "@formatjs/intl-getcanonicallocales/polyfill";
import "@formatjs/intl-locale/polyfill";
import "@formatjs/intl-pluralrules/polyfill";
import "@formatjs/intl-numberformat/polyfill";
import "@formatjs/intl-datetimeformat/polyfill";
import "@formatjs/intl-relativetimeformat/polyfill";
import "@formatjs/intl-relativetimeformat/locale-data/en";
import "@formatjs/intl-pluralrules/locale-data/en";
import "@formatjs/intl-numberformat/locale-data/en";
import "@formatjs/intl-datetimeformat/locale-data/en";
import "@formatjs/intl-datetimeformat/add-all-tz";

Component example:

const startDate = new Date("2024-09-22T14:00:00Z");
const endDate = new Date("2024-09-22T16:00:00Z");

const thisYear = startDate.getFullYear() === new Date().getFullYear();

const config = {
  use12HourTime: false,
};

return (
  <FormattedDateTimeRange
    from={startDate}
    to={endDate}
    weekday="short"
    day="numeric"
    month="short"
    hour="2-digit"
    minute="2-digit"
    hour12={config.use12HourTime}
    year={thisYear ? undefined : "numeric"}
  />
);

Expected behavior

  1. The date should not be duplicated; I expect it to be displayed as Sun, 22 Sept, 14:00–16:00 when the from and to dates are the same day.
  2. The time should be shown in the user's device timezone instead of UTC by default (I can get this working when removing the polyfills and using FormattedDate to test)

Smartphone:

  • Device: iPhone 12
  • OS: iOS 18.0
  • Browser: N/A
  • Version: react-intl 6.6.8

Additional context

Thanks in advance for helping, really appreciate what you guys are building!

Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the Stale label Oct 22, 2024
@JustJoostNL
Copy link
Author

JustJoostNL commented Oct 22, 2024

Still an issue. Any updates?

@github-actions github-actions bot removed the Stale label Oct 23, 2024
@longlho
Copy link
Member

longlho commented Nov 4, 2024

for the tz issue there's no way for JS to know default timezone so you have to set DateTimeFormat.__defaultTimeZone to the correct one

@JustJoostNL
Copy link
Author

Well, it does work perfectly when I remove the polyfills (and test with FormattedDate).

@longlho
Copy link
Member

longlho commented Nov 4, 2024

Because native browsers have access to that, JS does not. You can read the doc here https://formatjs.github.io/docs/polyfills/intl-datetimeformat#default-timezone

@JustJoostNL
Copy link
Author

Ah my bad, thanks for clarifying. Do you also have any idea on the second issue, the date and day that are duplicated?

@longlho
Copy link
Member

longlho commented Nov 4, 2024

It's unfortunately non-trivial to fix so I'll keep this open/pinned until I have time to fix it.

Copy link

github-actions bot commented Dec 5, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the Stale label Dec 5, 2024
@JustJoostNL
Copy link
Author

This is still an issue.

@github-actions github-actions bot removed the Stale label Dec 6, 2024
@longlho longlho added the pinned label Dec 9, 2024
@JustJoostNL
Copy link
Author

@longlho Is there any update on this? πŸ™‚

@longlho
Copy link
Member

longlho commented Feb 5, 2025

unfortunately nope :(

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

No branches or pull requests

2 participants