📜  日期到字符串格式 javascript - 任何代码示例

📅  最后修改于: 2022-03-11 14:57:06.835000             🧑  作者: Mango

代码示例6
function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

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

    return [year, month, day].join('-');
}
 
console.log(formatDate('Sun May 11,2014'));