본문 바로가기
javascript

Date getMonth 달 2자리로 출력하기

by 바나냥 2020. 9. 28.

2020/7/7 -> 2020/07/07

 

const month = date.getMonth() + 1;
return month < 10 ? `0${String(month)}` : `${String(month)}`;

const day = date.getDate();
return day < 10 ? `0${String(day)}` : `${String(day)}`;

 

 

stackoverflow.com/questions/6040515/how-do-i-get-month-and-date-of-javascript-in-2-digit-format

 

How do I get Month and Date of JavaScript in 2 digit format?

When we call getMonth() and getDate() on date object, we will get the single digit number. For example : For january, it displays 1, but I need to display it as 01. How to do that?

stackoverflow.com

 

댓글