3

When I was using jsc Engine, everything was working fine but when I shifted from jsc engine to Hermes Engine in my react native app I started getting Invalid date where I was using moment.

CODE:

const defaultDate = moment(currentTimeStamp).format("YYYY/MM/DD"); //till this it works fine
return moment(defaultDate).toDate().getTime(); // throws INVALID DATE
sakshya73
  • 5,570
  • 5
  • 22
  • 41

2 Answers2

5

After some R&D, I was able to figure out the issue myself so I thought to share it here.

Solution 1 :

Instead of using format like format("YYYY/MM/DD");. Use it like format("YYYY-MM-DD"); as this is one of the standard format that moment understands.

Solution 2:

If you need that format only(which was my case). You need to tell the moment the format that you are using. Like this:

return moment(defaultDate,"YYYY/MM/DD").toDate().getTime();
sakshya73
  • 5,570
  • 5
  • 22
  • 41
0

I ran into this issue when working on a project that was relying moment to parse the format. To get to moment to work with Hermes I had to explicit express the format as so moment(defaultDate,"YYYY/MM/DD"). So came with a more dynamic solution:

import moment from 'moment-timezone'; // or ''
const parseFormat = require('moment-parseformat');

/**
* @description This function will parse the date time string and return a moment object. This was created to fix a bug with Hermes JS engine.
* Explained here: https://stackoverflow.com/questions/71421582/moment-returning-an-invalid-date-while-using-hermes-js-engine and here: https://github.com/facebook/hermes/issues/865
* @param {string} dateTime - date time string
* @return {moment} - moment object
*/
export const momentWithFormatParser = (dateTime: string) => {
  if (typeof dateTime === 'string') {
    const test = moment(dateTime, parseFormat(dateTime));
    return test
  }
  return moment(dateTime);
}