最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React-Native reacts differently when not debugging remotely - Stack Overflow

programmeradmin1浏览0评论
<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>

Output when JS remote debugging is off

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

Output when JS remote debugging is on

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

What's causing this and how do I fix it?

<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>

Output when JS remote debugging is off

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

Output when JS remote debugging is on

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

What's causing this and how do I fix it?

Share Improve this question asked Feb 14, 2017 at 4:44 Saravanabalagi RamachandranSaravanabalagi Ramachandran 8,89912 gold badges58 silver badges112 bronze badges 2
  • Read the official document for more information, it explained it very well. As an alternative, you can use moment instead of JS date API. – Xeijp Commented Feb 14, 2017 at 5:35
  • Its better to use MomentJs – Ata Mohammadi Commented Feb 14, 2017 at 11:11
Add a ment  | 

2 Answers 2

Reset to default 11
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>

will consistently yield

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

Why doesn't the usual Date() way work?

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 municates 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.

So, as mentioned in the ments, it's better to use momentJS instead of relying blindly on the JS of the environment doing which can lead to inconsistencies as mentioned in the docs

I was facing the same problem till I started to use the following code:

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

Here I had used +02:00 since the server date is in GMT+2 timezone. You should change it to adjust your server date timezone

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>