简体   繁体   English

在 React Native 上没有调试模式的时刻格式时间将成为无效日期

[英]Moment format the time will become invalid date without debug mode on React Native

moment version is 2.24.0时刻版本是 2.24.0

My date value is 2019-04-23 03:16:00 +0000 UTC我的日期值为2019-04-23 03:16:00 +0000 UTC

I use moment to let it become to Asia time just like:我用 moment 让它变成亚洲时间,就像:

const moment = require('moment');

const localTime = moment(date).format('YYYY/MM/DD HH:mm');

<Text>{localTime}</Text>

localTime will show 2019/04/23 11:16 localTime 将显示2019/04/23 11:16

It works when I test it on debug mode.当我在调试模式下测试它时它起作用了。

But when I close the debug mode localTime will be但是当我关闭调试模式时, localTime将是

invalid date

The issue happen both of Android and IOS. Android和IOS都会出现这个问题。

Any ideas?有任何想法吗?

For anyone else experiencing date/time issues with Moment on Android especially if you're moving from React Native 0.59 (or older) to 0.60+, it appears that Hermes changes the way the Android works with Moment/dates.对于在 Android 上遇到 Moment 日期/时间问题的任何其他人,特别是如果您从 React Native 0.59(或更早版本)迁移到 0.60+,Hermes 似乎改变了 Android 使用 Moment/日期的方式。 However, it would work when the debugger was enabled.但是,它会在启用调试器时起作用。 Turns out, when you run the debugger, it switches back to the Chromium engine (or V8?) from Hermes.事实证明,当您运行调试器时,它会从 Hermes 切换回 Chromium 引擎(或 V8?)。 Resulted in us having to use console logs to track down Moment parsing issues.导致我们不得不使用控制台日志来追踪 Moment 解析问题。 Oddly, the issues also occurred when trying the same manipulations in Safari.奇怪的是,在 Safari 中尝试相同的操作时也会出现问题。

If you're parsing dates passed in via different vars for day, month, year and the day or month does not have preceding zeroes (ex: 01 vs 1) then I recommend doing this: const momentFormat = { y: birthYear, m: birthMonth, d: birthDay }; return Moment(momentFormat).format('MMMM Do, YYYY');如果您正在解析通过日、月、年的不同变量传入的日期,并且日或月没有前面的零(例如:01 vs 1),那么我建议这样做: const momentFormat = { y: birthYear, m: birthMonth, d: birthDay }; return Moment(momentFormat).format('MMMM Do, YYYY'); const momentFormat = { y: birthYear, m: birthMonth, d: birthDay }; return Moment(momentFormat).format('MMMM Do, YYYY'); This manually sets the values rather than relying on each value being correctly formatted or having to write custom code to ensure the day/month values have the 0 when needed.这手动设置值,而不是依赖于正确格式化的每个值或必须编写自定义代码以确保日/月值在需要时具有 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM