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

Member

by lottie , in category: Javascript , 22 days ago

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

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by muriel.schmidt , 22 days 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.