Get the time zone with React Native

I found it is better to get the actual timezone ID (such as America/New_York) and then set your server program to work using that timezone at the beginning. To get that, use the react-native-device-info library: https://www.npmjs.com/package/react-native-device-info

import DeviceInfo from 'react-native-device-info';

console.log(DeviceInfo.getTimezone());   //   'America/New_York'

If you get an error about 'tvOS' during compilation, see this fix: https://github.com/rebeccahughes/react-native-device-info/issues/286


Pretty much anything you could do in JavaScript on the browser (with exception, of course), you can do in React Native. I would initiate a new date object and call getTimezoneOffset() on it to get the number of minutes the timezone is offset locally.

var date = new Date();
var offsetInHours = date.getTimezoneOffset() / 60;

I also needed to get the time zone in my React Native App. But my needs required to get a timezone in IANA format rather than the offset.

The library suggested above "react-native-device-info", deprecated the timeZone method and therefore I had to search for updated alternatives.

I'm using expo and I had problems using "react-intl" library, but I found two libraries that can do the job:

  1. If you're using "bare" react native:
  • "react-native-localize" library is a good option.

  • https://github.com/zoontek/react-native-localize

console.log(RNLocalize.getTimeZone());
// -> "Europe/Paris" 

  1. If you're using expo:
  • "expo-localization" is the best option.
  • https://docs.expo.io/versions/latest/sdk/localization/
 console.log(Localization.timezone);
// -> "Europe/Paris" 

Tags:

React Native