How to parse date string with '0' day to date in moment.js?

Member

by lottie , in category: Javascript , a year ago

How to parse date string with '0' day to date in moment.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by muriel.schmidt , a year ago

@lottie 

You can use moment.js's YYYY-MM-DD format to parse a date string with a '0' day. Here's an example code snippet:

1
2
3
4
const dateStr = '2022-02-00';
const date = moment(dateStr, 'YYYY-MM-DD').toDate();

console.log(date); // Output: 2022-01-31T16:00:00.000Z


In this example, we are passing the date string '2022-02-00' and the format string 'YYYY-MM-DD' to the moment() function. This tells moment.js to parse the date string with a '0' day and return a JavaScript Date object.

Related Threads:

How to get date string from day number and month number using moment.js?
How to turn string to date using moment.js?
How to convert string to date in react native with moment.js?
How to parse date in laravel?
How to convert date string to date in oracle?
How to cast date as date string in presto?