Javascript add leading zeroes to date

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

    You can use String.slice() which extracts a section of a string and returns it as a new string, without modifying the original string:

    const currentDate = new Date().toISOString().slice(0, 10) // 2020-04-16
    

    Or you can also use a lib such as Moment.js to format the date:

    const moment = require("moment")
    const currentDate = moment().format("YYYY-MM-DD") // 2020-04-16
    

提交回复
热议问题