Format date as yyyy-mm-dd using JavaScript

 

To format date in to your desired format use getFullYear(), getMonth() and getDate() methods to get the year, month and day of the date.

 

    var d = new Date('Sun May 11,2022'),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) 
        month = '0' + month;
    if (day.length < 2) 
        day = '0' + day;

    var formateddate = [year, month, day].join('-');
    
    console.log( formateddate );

 


Tags:

Share:

Related posts