Javascript date - Leading 0 for days and months where applicable

前端 未结 9 2022
情话喂你
情话喂你 2020-12-15 05:07

Is there a clean way of adding a 0 in front of the day or month when the day or month is less than 10:

var myDate = new Date();
var prettyDate =(myDate.getFu         


        
9条回答
  •  醉梦人生
    2020-12-15 05:51

    You can try like this

    For day:

    ("0" + new Date().getDate()).slice(-2)
    

    For month:

    ("0" + (new Date().getMonth() + 1)).slice(-2)
    

    For year:

    new Date().getFullYear();
    

提交回复
热议问题