Odaiah Odaiah - 6 months ago 99
Javascript Question

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?

Answer
("0" + this.getDate()).slice(-2)

for the date, and similar:

("0" + (this.getMonth() + 1)).slice(-2)

for the month.