Js standard time format

Mon Jun 25 2018 15:19:07 GMT+0800

how is this time formatted as year, month, day, 2018-06-25

is there a direct js method that is not plug-in type

Mar.21,2021

//:yyyy-MM-dd
function formatDate(date) {
    var myyear = date.getFullYear();
    var mymonth = date.getMonth() + 1;
    var myweekday = date.getDate();
 
    if (mymonth < 10) {
        mymonth = "0" + mymonth;
    }
    if (myweekday < 10) {
        myweekday = "0" + myweekday;
    }
    return (myyear + "-" + mymonth + "-" + myweekday);
}

var date = new Date();
//date
//Mon Jun 25 2018 15:32:38 GMT+0800 ()
formatDate(date);
//"2018-06-25"

for more, please see js to get the date and the accumulation summary of date-related js methods


var date = new Date('Mon Jun 25 2018 15:19:07 GMT+0800');
console.log(date.toLocaleDateString().replace(/\//g,'-'));

function formatDate(dateArg) {
    const date = new Date(dateArg);
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const formatMonth = month < 10 ? `0${month}` : month;
    const formatDay = day < 10 ? `0${day}` : day;

    return `${year}-${formatMonth}-${formatDay}`
}

var date = new Date('2018/6/25')
date.toLocaleDateString('cn',{year: 'numeric', month: '2-digit', day: '2-digit' }).replace(/\//g, '-')
Menu