const dateFormat = (dateInput: Date, format: string): string => {
var day = dateInput.getDate()
var month = dateInput.getMonth() + 1
var year = dateInput.getFullYear();
format = format.replace(/yyyy/, year.toString())
format = format.replace(/MM/, tofill(month).toString())
format = format.replace(/dd/, tofill(day).toString())
return format
}
const tofill = (num: number) => {
return num < 10 ? ("0" + num) : num
}
console.log(dateFormat(new Date('2020-11-1'), 'yyyy年MM月dd日'))
js日期格式化函数
最新推荐文章于 2024-10-07 10:25:02 发布