Reputation: 126
I have a mobile application made with expo SDK 45, which its main feature is related to times,
So I opted to use date-fns to manage and format the time in the application... Unfortunately for some TimeZone, the format()
method does not return the date selected in the calendar.
For example, being in the United States/NewYork timeZone, if I select 2022-06-23
in the calendar and I pass it in the format()
method it gives me 2022-06-22
But if I enter in Europe/Paris timeZone everything works fine...
I did some research and came across date-fns-tz, which is a support for data-fns to handle timeZones. the problem, when I try to use the formatInTimeZone()
method for example, I get this error every time: 'Invalid time zone specified: ...'
But I'm sure I've implemented the code correctly from the documentation...
I.e:
const { formatInTimeZone } = require('date-fns-tz')
const date = new Date('2014-10-25T10:46:20Z')
formatInTimeZone(date, 'America/New_York', 'yyyy-MM-dd HH:mm:ssXXX') // 2014-10-25 06:46:20-04:00
The above code systematically returns an error...
Upvotes: 2
Views: 2252
Reputation: 126
I was able to solve my problem by installing date-time-format-timezone as a dependency and importing it from the root of my application
Currently my app entry file (App.tsx) looks like this:
import 'intl';
import 'intl/locale-data/jsonp/en-US';
import 'date-time-format-timezone';
import App from './src/App';
export default App;
Upvotes: 1