Javascript add leading zeroes to date

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

    There is another approach to solve this problem, using slice in JavaScript.

    var d = new Date();
    var datestring = d.getFullYear() + "-" + ("0"+(d.getMonth()+1)).slice(-2) +"-"+("0" + d.getDate()).slice(-2);
    
    

    the datestring return date with format as you expect: 2019-09-01

    another approach is using dateformat library: https://github.com/felixge/node-dateformat

提交回复
热议问题