Javascript add leading zeroes to date

后端 未结 25 2231
执笔经年
执笔经年 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:24

    function formatDate(jsDate){
      // add leading zeroes to jsDate when days or months are < 10.. 
      // i.e.
      //     formatDate(new Date("1/3/2013")); 
      // returns
      //    "01/03/2103"
      ////////////////////
      return (jsDate.getDate()<10?("0"+jsDate.getDate()):jsDate.getDate()) + "/" + 
          ((jsDate.getMonth()+1)<10?("0"+(jsDate.getMonth()+1)):(jsDate.getMonth()+1)) + "/" + 
          jsDate.getFullYear();
    }
    

提交回复
热议问题