Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the time zone with React Native

Tags:

react-native

I'd like to get the timezone of the phone user so I can add or substract it to the UTC times I get from my API, and then make operations on them (for notifications, for example).

I only found how to add an offset with DatePickerIOS, but nothing else... Is it possible to get it from the front?

like image 946
Thanh-Quy Nguyen Avatar asked May 31 '16 18:05

Thanh-Quy Nguyen


People also ask

How do I get the timezone in react native?

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.

How do I find moment time zone?

var tz = moment. tz. guess(); It will return an IANA time zone identifier, such as America/Los_Angeles for the US Pacific time zone.


1 Answers

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

like image 149
user295145 Avatar answered Oct 10 '22 07:10

user295145