结果:
/** * * @param {*} formater 时间格式 * @param {*} time 自定时间,默认当前时间 * @returns */ const dateFormater = (formater, time) => { let date = time ? new Date(time) : new Date(), Y = date.getFullYear() + '', M = date.getMonth() + 1, D = date.getDate(), H = date.getHours(), m = date.getMinutes(), s = date.getSeconds(); return formater.replace(/YYYY|yyyy/g, Y) .replace(/YY|yy/g, Y.substr(2, 2)) .replace(/MM/g,(M<10 ? '0' : '') + M) .replace(/DD/g,(D<10 ? '0' : '') + D) .replace(/HH|hh/g,(H<10 ? '0' : '') + H) .replace(/mm/g,(m<10 ? '0' : '') + m) .replace(/ss/g,(s<10 ? '0' : '') + s) } console.log(dateFormater('YYYY -MM-DD')) console.log(dateFormater('YYYY-MM-DD HH')) console.log(dateFormater('YYYY-MM-DD HH:mm')) console.log(dateFormater('YYYY-MM-DD HH:mm:ss')) console.log(dateFormater('YYYYMMDDHHmmss'))