How to use getMonth() with 2 digit format in Node.js

1 Answer

0 votes
function getMonth2DigitsFormat(date) {
    const month = date.getMonth() + 1;
 
    if (month < 10) {
        return '0' + month;
    }
 
    return month;
}
 
const date = new Date('June 17, 2022 03:18:05');
 
console.log(getMonth2DigitsFormat(date));
 
 
    
    
    
    
/*
run:
 
06
    
*/

 



answered Jun 18, 2022 by avibootz
edited Jun 18, 2022 by avibootz

Related questions

...