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

1 Answer

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

 



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