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?
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 tz = moment. tz. guess(); It will return an IANA time zone identifier, such as America/Los_Angeles for the US Pacific time zone.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With