react-native getTimezoneOffset 仅适用于调试模式
react-native getTimezoneOffset works only on debug mode
我当前的时区是 GMT+1。最近,我注意到有时我得到 -60 值,而其他时候我得到 0.
componentDidMount() {
this.props.screenProps.showToast(new Date().getTimezoneOffset(), 1);
}
现在我意识到,当我在 chrome 上调试它时,我得到了正确的时区,每当我在 chrome 中关闭远程调试时,我得到的偏移值为 0。
似乎 Date() 对象的操作因环境而异。
我正在测试的设备是android。另外,在 ios 上会有什么不同吗?
任何对此的澄清都会很有帮助。
我认为这可能与您遇到的情况有关。
JavaScript Runtime When using React Native, you’re going to be running your JavaScript code in two environments:
On iOS simulators and devices, Android emulators and devices React Native uses JavaScriptCore which is the JavaScript engine that powers Safari. On iOS JSC doesn’t use JIT due to the absence of writable executable memory in iOS apps.
When using Chrome debugging, it runs all the JavaScript code within Chrome itself and communicates with native code via WebSocket. So you are using V8.
我认为这会导致远程调试 off/on 时出现不一致。查找更多信息 here。
尝试只显示当前时间。如果它仍然显示错误的时间,那么问题出在那些模拟器 运行 的 JS 引擎上。尝试 moment JS 或任何其他轻量级时间库,如果它们适用于您的情况。
我当前的时区是 GMT+1。最近,我注意到有时我得到 -60 值,而其他时候我得到 0.
componentDidMount() {
this.props.screenProps.showToast(new Date().getTimezoneOffset(), 1);
}
现在我意识到,当我在 chrome 上调试它时,我得到了正确的时区,每当我在 chrome 中关闭远程调试时,我得到的偏移值为 0。
似乎 Date() 对象的操作因环境而异。
我正在测试的设备是android。另外,在 ios 上会有什么不同吗?
任何对此的澄清都会很有帮助。
我认为这可能与您遇到的情况有关。
JavaScript Runtime When using React Native, you’re going to be running your JavaScript code in two environments: On iOS simulators and devices, Android emulators and devices React Native uses JavaScriptCore which is the JavaScript engine that powers Safari. On iOS JSC doesn’t use JIT due to the absence of writable executable memory in iOS apps. When using Chrome debugging, it runs all the JavaScript code within Chrome itself and communicates with native code via WebSocket. So you are using V8.
我认为这会导致远程调试 off/on 时出现不一致。查找更多信息 here。
尝试只显示当前时间。如果它仍然显示错误的时间,那么问题出在那些模拟器 运行 的 JS 引擎上。尝试 moment JS 或任何其他轻量级时间库,如果它们适用于您的情况。