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

RangeError: Invalid time value #8

Closed
CoderCharlie opened this issue Sep 22, 2024 · 7 comments · Fixed by #12
Closed

RangeError: Invalid time value #8

CoderCharlie opened this issue Sep 22, 2024 · 7 comments · Fixed by #12

Comments

@CoderCharlie
Copy link

const now = new Date()
const nowTZ = TZDate.tz("America/New_York", now)
console.log(nowTZ.toString())

I changed it to a few other examples, still got the same error as below

RangeError: Invalid time value
@kossnocorp
Copy link
Member

Hey! There's not enough info to do something with it. The environment, the version, your time zone, and so on.

image

@CoderCharlie
Copy link
Author

{
  "@date-fns/tz": "^1.0.2",
  "date-fns": "^4.1.0",
  "react-native": "0.73.4"
  ...
}

I was running a react-native app on my iPhone 15 simulator with iOS 17.4 on MacOS 14.3.1 (23D60) with 2018 Macbook Pro

@kevin-abiera
Copy link
Contributor

This is a reproduction of this bug: https://codesandbox.io/p/sandbox/datetimeformat-polyfill-timezone-forked-rkdyrp?file=%2Fsrc%2Findex.js%3A20%2C32

The hour: 'numeric' nor the timeZoneName isn't respected by the polyfill.

Would you consider updating

const offsetStr = format(date).slice(6);
to something like the following?

const offsetStr = format(date).split('GMT')[1];

@kossnocorp
Copy link
Member

kossnocorp commented Sep 23, 2024

@CoderCharlie, can you please check what this code returns in your environment:

new Intl.DateTimeFormat("en-GB", { timeZone: "Asia/Singapore", hour: "numeric", timeZoneName: "longOffset" }).format(new Date())

I don't have access to the simulator and React Native to test, but @kevin-abiera might be right because of the limited Intl support.

@kevin-abiera I guess there must be a way, but what you suggested doesn't work without hour and timeZoneName. I don't mind supporting the polyfill, but where do I look to see what is actually supported? I would appreciate a PR.

@kevin-abiera
Copy link
Contributor

In react-native using expo, that would return something like: "4 pm GMT+08:00". You can test here: https://snack.expo.dev/@lightning18/talking-blue-apple and make sure to switch to "iOS" device on the right. Notice the poor support of timezones hence the need for polyfill. I can't seem to make the polyfill work in snack expo atm but you can test the output here: https://codesandbox.io/p/sandbox/datetimeformat-polyfill-timezone-forked-rkdyrp?file=%2Fsrc%2Findex.js%3A1%2C1-5%2C51. It just always returns something like: "5:53 PM GMT-9:30"

I don't think we need the hour here before the GMT? or do we?

@kevin-abiera
Copy link
Contributor

@kossnocorp I created #12. Let me know what you think 🙏🏻

@kossnocorp
Copy link
Member

Thank you, @kevin-abiera! I shipped the fix with @date-fns/[email protected]

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

Successfully merging a pull request may close this issue.

3 participants