不进行远程调试时,React-Native 的反应不同

React-Native reacts differently when not debugging remotely

<Text style={s.date}>{ new Date(this.props.order.ordered_at).toLocaleDateString('en-US', { day: '2-digit', month: 'short' }) }</Text>
<Text style={s.time}>({ new Date(this.props.order.ordered_at).toLocaleTimeString('en-US', { hour: '2-digit', minute:'2-digit', hour12: true }) })</Text>

JS远程调试关闭时的输出

02/13/17 (23:51:31)
02/13/17 (23:48:07)

开启JS远程调试时的输出

Feb 13 (11:51 PM)
Feb 13 (11:48 PM)

这是什么原因造成的,我该如何解决?

import moment from 'moment';

<Text style={s.date}>{ moment(new Date(this.props.order.ordered_at)).format('ll') }</Text>
<Text style={s.time}>{ moment(new Date(this.props.order.ordered_at)).format('LT') }</Text>

将始终如一地产生

Feb 13, 2017 (11:51 PM)
Feb 13, 2017 (11:48 PM)

为什么通常的 Date() 方法不起作用?

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.

While both environments are very similar, you may end up hitting some inconsistencies. We're likely going to experiment with other JS engines in the future, so it's best to avoid relying on specifics of any runtime.

因此,如评论中所述,最好使用 momentJS 而不是盲目依赖环境的 JS 这样做会导致 docs[=16] 中提到的不一致=]

在开始使用以下代码之前,我一直面临同样的问题:

moment(`${post.date}+02:00`).

这里我使用了 +02:00 因为服务器日期是 GMT+2 时区。您应该更改它以调整您的服务器日期时区