constWEEK_ARR=['周日','周一','周二','周三','周四','周五','周六']exportclassDateFormat{
date
constructor(date =newDate()){this.date =this.getDate(date)}getTimestamp(days){return days *24*60*60*1000}add(count, type ='day'){switch(type){case'day':this.date =newDate(+this.date +this.getTimestamp(count))breakcase'week':this.date =newDate(+this.date +this.getTimestamp(count *7))breakcase'month':const thisMonth =this.date.getMonth()// const year = (thisMonth + count)/12 >> 0// const month = (thisMonth + count) % 12// this.date.setYear(this.date.getFullYear()+year)this.date.setMonth(thisMonth + count)breakdefault:}returnthis}/*
* @date 可以是时间戳,字符串,Date对象
*/getDate(date){if(!date){returnnewDate()}if(date instanceofDate){return date
}if(isNaN(date)&&typeof date ==='string'){// 2017-01 || 2017/1if(/^\d{4}-\d{2}$/.test(date)||/^\d{4}\/\d{2}$/.test(date)){
date =`${date}-01`}returnnewDate(date.replace(/-/g,'/'))}if(typeof+ date ==='number'){returnnewDate(date)}}isSame(time, type ='day'){if(type ==='day'){returnthis.format('YYYY-MM-DD')=== time.format('YYYY-MM-DD')}elseif(type ==='month'){returnthis.format('YYYY-MM')=== time.format('YYYY-MM')}else{returnthis.format('YYYY')=== time.format('YYYY')}}//timestamptimestamp(){return+this.date
}//本月第几天today(){returnthis.date.getDate()}//本周第几天day(){returnthis.date.getDay()}month(){returnthis.date.getMonth()}//当月有几天daysInMonth(){const date =newDate()const currMonth =this.date.getMonth()
date.setMonth(currMonth +1)
date.setDate(0);return date.getDate();}getWeekString(){returnWEEK_ARR[this.date.getDay()]}getWeekIndex(){returnthis.date.getDay()}format(format){if(format.key ==2){if(format.time){let kong =''let k =""var d =newDate(format.time)if(d.getMonth()+1<10){
kong =0}else{
kong =''}if(d.getDate()<10){
k =0}else{
k =""}var datetime =`${d.getFullYear()}${kong}${(d.getMonth()+1)}${k}${d.getDate()}`;return datetime
}else{return''}}else{const year =this.date.getFullYear()const month =this.date.getMonth()+1const day =this.date.getDate()//月历考勤const signMonth =this.date.getMonth()const fullMonth = signMonth >9? signMonth : signMonth ==0?12:`0${signMonth}`const hour =this.date.getHours()const minute =this.date.getMinutes()const second =this.date.getSeconds()const monthStr = month >9? month :`0${month}`const dayStr = day >9? day :`0${day}`const hourStr = hour >9? hour :`0${hour}`const minuteStr = minute >9? minute :`0${minute}`const secondStr = second >9? second :`0${second}`let result =''switch(format){case'YYYY':
result = year
breakcase'MM':
result = monthStr
breakcase'M':
result = month
breakcase'DD':
result = dayStr
breakcase'D':
result = day
breakcase'HH':
result = hourStr
breakcase'H':
result = hour
breakcase'mm':
result = minuteStr
breakcase'm':
result = minute
breakcase'ss':
result = secondStr
breakcase's':
result = second
breakcase'HH:mm':
result =`${hourStr}:${minuteStr}`breakcase'HH:mm:ss':
result =`${hourStr}:${minuteStr}:${secondStr}`break//6.12月历考勤新增case'YYYY-MM-R':
result = signMonth ==0?`${year -1}-${fullMonth}`:`${year}-${fullMonth}`breakcase'YYYYMM':
result =`${year}${monthStr}`breakcase'YYYYMMDD':
result =`${year}${monthStr}${dayStr}`breakcase'YYYY-MM':
result =`${year}-${monthStr}`breakcase'YYYY-MM-DD':
result =`${year}-${monthStr}-${dayStr}`breakcase'YYYY/MM/DD':
result =`${year}/${monthStr}/${dayStr}`breakcase'YYYY-M-D':
result =`${year}-${month}-${day}`breakcase'YYYY-MM-DD HH:mm':
result =`${year}-${monthStr}-${dayStr}${hourStr}:${minuteStr}`breakcase'YYYY-MM-DD HH:mm:ss':
result =`${year}-${monthStr}-${dayStr}${hourStr}:${minuteStr}:${secondStr}`breakcase'YYYY-M-D H:m:s':
result =`${year}-${month}-${day}${hour}:${minute}:${second}`breakcase'YYYY-MM-DD 00:00:00':
result =`${year}-${monthStr}-${dayStr} 00:00:00`breakcase'MM月':
result =`${monthStr}月`breakcase'M月D日':
result =`${month}月${day}日`breakcase'M.D':
result =`${month}.${day}`breakcase'MM.DD':
result =`${monthStr}.${dayStr}`breakcase'YYYY/MM/DD':
result =`${year}/${monthStr}/${dayStr}`breakcase'YYYY/MM/DD HH:mm':
result =`${year}/${monthStr}/${dayStr}${hourStr}:${minuteStr}`breakcase'MM/DD HH:mm':
result =`${monthStr}/${dayStr}${hourStr}:${minuteStr}`breakcase'MM月DD日':
result =`${month}月${day}日`breakcase'YYYY年MM月DD日':
result =`${year}年${month}月${day}日`breakcase'YYYY年MM月':
result =`${year}年${monthStr}月`breakcase'MM月DD日 dddd':
result =`${monthStr}月${dayStr}日 ${this.getWeekString()}`breakcase'YYYY年MM月DD日 dddd':
result =`${year}年${monthStr}月${dayStr}日 ${this.getWeekString()}`breakcase'MM月DD日 HH:mm':
result =`${monthStr}月${dayStr}日 ${hourStr}:${minuteStr}`breakcase'YYYY年MM月DD日 HH:mm':
result =`${year}年${monthStr}月${dayStr}日 ${hourStr}:${minuteStr}`breakcase'YYYY年MM月DD日 HH:mm:ss':
result =`${year}年${monthStr}月${dayStr}日 ${hourStr}:${minuteStr}:${secondStr}`break}return result
}}}exportconstdateFormat=(date)=>newDateFormat(date)exportconstgetMonthDays=(date)=>{let days
let year = date.split('-')[0]let month = date.split('-')[1]if(month ==='02'){
days = year %4==0?29:28;}elseif(month =='01'|| month =='03'|| month =='05'|| month =='07'|| month =='08'|| month =='10'|| month =='12'){
days =31}else{
days =30}return days
}