react-native: Time zone offset not detected on Android device
Is this a bug report?
Yes
Have you read the Contributing Guidelines?
Yes
Environment
react-native -v
: 0.46.0node -v
:v8.2.1npm -v
:3.10.10yarn --version
:0.27.5
Then, specify:
- Target Platform: iOS and Android
- Development Operating System: macOs sierra 10.12.6
- Build tools: Webstorm, issue only happens on Android devices (my app targets Android 7.0 and highers).
Steps to Reproduce
(Write your steps here:)
- Set your device time with a timezone offset (GMT +2 in my case);
- create a new Date (const date = new Date()š
- call getTimezoneOffset() on your date.
Expected Behavior
I expected my time zone offset to have 120 as value.
Actual Behavior
I get a ā0ā offset, as if my device timezone had not been detected. The only way to get my timezone detected is to add a call to ātoLocaleString()ā function on my date, and then, my timezone offset is correct. I donāt undersand the interactions between āgetTimezoneOffset()ā and ātoLocaleString()ā.
Reproducible Demo
(Paste the link to an example project and exact instructions to reproduce the issue.)
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 19
- Comments: 16 (4 by maintainers)
Hello there, this issue should be re-opened š I can confirm this issue happens on Samsung devices (tested on Galaxy S7 & S8). On a Nexus 5X, there is no problem. Tested on RN v.0.50.3
š for re-opening the issue. Happens on Motorola devices too (along with the devices reported earlier). Tested till RN v0.51.0 Considering a large number of devices reported, this must be fixed asap.
I have noticed this, and it doesnāt happen on all Android devices. When it does happen though, itās a really bad bug for the user.
For my app, I can consistently reproduce on a Samsung Galaxy S7 with Android 7.0 installed. Iām aware that some of my appās users have also experienced it on Huawei devices. All times displayed to the user in my app are off by three hours into the future. The device shows the correct time in the OS home screen, but times in the React Native app are off.
We are using React Native 49. Restarting the React Native app does not resolve the issue.
I am facing another weird problem with new Date() in some android devices only, not in iOS.
Suppose current time in my phone is 11:55 AM (GMT+5:30). Initially new Date( ).getMinutes() returns correct minutes (55 in this case), but when I go to Date & Time Settings in my Phone and change the time zone to GMT-04:00 (2:26 AM) ,I still get the old time in console (Reactotron console). Restarting the phone apparently seem to fix this bug.
Please note that I am not using chrome debugging for logging current time. I am using Reactotron for logging. I am using RN 0.55.4 and android device MI Redmi 4. But this is happening on Samsung devices as well.
I have tried using moment also but that too didnāt help. I donāt want to write a native module for such a trivial task
Any thoughts?
We just followed what was posted by @victoriaBoucher and for us it worked quite well:
So we just put this line of code in the file that defines our outermost <App /> component, so that it is run as soon as the App is imported:
And that fixed things. No idea why, and if it handles all the scenarios, but we didnāt have any issues after that.
This issue is always reproducible in Samsung devices.
Same here, iām using RN v0.55.3 on Android 7 and zebra devices. When app is in debug this issue happens almost alway in the fist 15 minutes, after this time the app get right time.
I (actually my customer š¦ ) am having this issue with RN v0.53.3 on Android.
I can also confirm this. Only way to fix it after this happens is to reboot the phone.