snippet

How to get month in mm (with zero) format in javascript ?

In Javascript it is possible to retrieve the month according to an indicated date, the return value is an integer and our a string. So if you want to display the date always in mm format you have to use this little trick to force the zero on the months between January and September.
const mm = (new Date(2022, 1, 1).getMonth() + 1).toString().padStart(2, '0')
// return 03 value
Related snippets