How to use getMonth() with 2 digit format in JavaScript

1 Answer

0 votes
function getMonth2DigitsFormat(date) {
    const month = date.getMonth() + 1;
 
    if (month < 10) {
        return '0' + month;
    }
 
    return month;
}
 
const date = new Date('April 21, 2022 06:13:05');
 
console.log(getMonth2DigitsFormat(date));
 
 
    
    
    
    
/*
run:
 
"04"
    
*/

 



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