js 手写日期转换函数
js实现日期转换函数
用yyyy,MM,dd固定字符串做替换
const dateFormat = (date, formatter) => {
const fDate = new Date(date)
const day = fDate.getDate()
const month = fDate.getMonth() + 1
const year = fDate.getFullYear()
return formatter.replace(/dd/, day).replace(/MM/, month).replace(/yyyy/, year)
}
console.log(dateFormat(new Date('2020-01-03'), 'yyyy年MM月dd日'));