Javascript add leading zeroes to date

后端 未结 25 1858
执笔经年
执笔经年 2020-11-22 02:50

I\'ve created this script to calculate the date for 10 days in advance in the format of dd/mm/yyyy:

var MyDate = new Date();
var MyDateString = new Date();
M         


        
相关标签:
25条回答
  • 2020-11-22 03:44

    Here is very simple example how you can handle this situation.

    var mydate = new Date();
    
    var month = (mydate.getMonth().toString().length < 2 ? "0"+mydate.getMonth().toString() :mydate.getMonth());
    
    var date = (mydate.getDate().toString().length < 2 ? "0"+mydate.getDate().toString() :mydate.getDate());
    
    var year = mydate.getFullYear();
    
    console.log("Format Y-m-d : ",year+"-"+month+"-" + date);
    
    console.log("Format Y/m/d : ",year+"/"+month+"/" + date);

    0 讨论(0)
提交回复
热议问题