0

I have the following function, which receives the date string and then checks if it is v alid moment date and then converts it to a JavaScript Date object.

const dateFromString = (value, locale) => {
  if (typeof value === 'string') {
    const dateMoment = moment(value, ['L LTS'], locale);
    return dateMoment.isValid() ? dateMoment.toDate() : null;
  }
  return value;
};

I also have a function which converts the Moment Date to String,

const dateToString = (value, locale) => {
  const dateMoment = moment(value);
  return dateMoment.isValid() ? dateMoment.format(currentDateFormat(locale, true)) : '';
};

Now, I need a function that can convert moment date to javascript date considering the locale, is there a way to do it?

tRuEsAtM
  • 3,517
  • 6
  • 43
  • 83
  • 1
    javascript dates don't hold locale information, they simply store an epoch offset in ms. – pilchard Dec 11 '22 at 22:36
  • See if this helps https://stackoverflow.com/questions/17987647/moment-js-transform-to-date-object – Matthew Dec 11 '22 at 22:47
  • 1
    Does this answer your question? [Moment.js transform to date object](https://stackoverflow.com/questions/17987647/moment-js-transform-to-date-object) – Matthew Dec 11 '22 at 22:48

0 Answers0