简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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