React Native:没有调试模式的时刻不适用于特定时间值 [英] React native : moment not working for specific time value without debug mode

查看:58
本文介绍了React Native:没有调试模式的时刻不适用于特定时间值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的代码中将时间值格式化为日期格式.

 const 到达日期时间 = 时刻(`${todaysDate.format("YYYY-MM-DD")} ${arrivalTime}:00:00`,).迄今为止();

当调试模式关闭时,如果我从控件中选择 8 或 9 值,则无法格式化该值.

当我在调试模式下,选择 8 或 9 时,它能够将值格式化为:

2020 年 5 月 22 日星期五 09:00:00 GMT-0600(山区夏令时)

我已经看到许多讨论同一问题的线程,但其中提供的解决方案并没有帮助我正确地格式化它.

我正在尝试打印到达日期时间值,它在日志中显示如下;

日期 { NaN }

我正在尝试这个,但它不起作用,天 toDate 不是一个函数:

moment().format(`YYYY-MM-DD ${arrivalTime}:00:00`).toDate();

解决方案

我花了一段时间,但我终于弄明白了.

这对我有帮助

这不起作用:

 const 到达日期时间 = 时刻(`${todaysDate.format("YYYY-MM-DD")} ${arrivalTime}:00:00`,).迄今为止();

这有效:

const 到达日期时间 = 时刻(`${todaysDate.format('YYYY/MM/DD')} ${arrivalTime}:00`,).迄今为止();

//注意引擎似乎不喜欢用' - '解析.所以我把它改成'/'

仍然不确定背后的原因.

I have below code where I format the time value to be date format.

 const arrivalDateTime = moment(
   `${todaysDate.format("YYYY-MM-DD")} ${arrivalTime}:00:00`,
 ).toDate();

When debug mode is off, if I select 8 or 9 value from the control, its not able to format the value.

When I am in debug mode, and select 8 or 9, its able to format the value as:

Fri May 22 2020 09:00:00 GMT-0600 (Mountain Daylight Time)

I have seen many threads discussing the same issue but solution provided in them haven not helped me to format this correctly.

I am trying to print arrivalDateTime value, it shows like this in log ;

Date { NaN }

I am trying this but it does not work, it days toDate is not a function :

moment().format(`YYYY-MM-DD ${arrivalTime}:00:00`).toDate();

解决方案

Took me a while but I finally figured it out.

this helped me

This didn't work:

 const arrivalDateTime = moment(
   `${todaysDate.format("YYYY-MM-DD")} ${arrivalTime}:00:00`,
 ).toDate();

This worked:

const arrivalDateTime = moment(
  `${todaysDate.format('YYYY/MM/DD')} ${arrivalTime}:00`,
).toDate();

// note that engine does not seem to like parsing with ' - '. so I changed it to ' / '

still not sure the reason behind it.

这篇关于React Native:没有调试模式的时刻不适用于特定时间值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆