SOURCE

/**
 * 输入:
 * dateFormat(new Date('2020-12-01'), 'yyyy/MM/dd') // 2020/12/01
 * dateFormat(new Date('2020-04-01'), 'yyyy/MM/dd') // 2020/04/01
 * dateFormat(new Date('2020-04-01'), 'M/d/yyyy') // 4/1/2020
 * dateFormat(new Date('2020-04-01'), 'yyyy年MM月dd日') // 2020年04月01日
 */
//const dateFormat = (date, reg) => {
    // code here
//};


const dateFormat = (date, reg) => {
    const o = {
        y: String(date.getFullYear()),
        M: String(date.getMonth() + 1),
        d: String(date.getDate()),
    };

    return reg.replace(/([yMd])+/g, ($1, key) =>
        o[key].padStart($1.length, '0').slice(-$1.length),
    );
};

console.log(dateFormat(new Date('2020-12-01'), 'yyyy/MM/dd'))
console.log(dateFormat(new Date('2020-04-01'), 'yyyy/MM/dd'))
console.log(dateFormat(new Date('2020-04-01'), 'M/d/yyyy'))
console.log(dateFormat(new Date('2020-04-01'), 'yyyy年MM月dd日'))
console 命令行工具 X clear

                    
>
console