Javascript add leading zeroes to date

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

    As @John Henckel suggests, starting using the toISOString() method makes things easier

    const dateString = new Date().toISOString().split('-');
    const year = dateString[0];
    const month = dateString[1];
    const day = dateString[2].split('T')[0];
    
    console.log(`${year}-${month}-${day}`);

提交回复
热议问题