React native : moment not working for specific time value without debug mode

徘徊边缘 提交于 2020-07-14 11:57:27

问题


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();

回答1:


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.



来源:https://stackoverflow.com/questions/61967235/react-native-moment-not-working-for-specific-time-value-without-debug-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!