dateFormat.js 常用日期格式转换
const WEEK_ARR = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
export class DateFormat {
date
constructor(date = new Date()) {
this.date = this.getDate(date)
}
getTimestamp(days) {
return days * 24 * 60 * 60 * 1000
}
add(count, type = 'day') {
switch (type) {
case 'day':
this.date = new Date(+this.date + this.getTimestamp(count))
break
case 'week':
this.date = new Date(+this.date + this.getTimestamp(count * 7))
break
case 'month':
const thisMonth = this.date.getMonth()
this.date.setMonth(thisMonth + count)
break
default:
}
return this
}
getDate(date) {
if(!date) {
return new Date()
}
if(date instanceof Date ) {
return date
}
if(isNaN(date) && typeof date === 'string') {
if(/^\d{4}-\d{2}$/.test(date) || /^\d{4}\/\d{2}$/.test(date)) {
date = `${
date}-01`
}
return new Date(date.replace(/-/g, '/'))
}
if(typeof +date === 'number') {
return new Date(date)
}
}
isSame(time, type = 'day') {
if(type === 'day') {
return this.format('YYYY-MM-DD') === time.format