type DateType = 'y' | 'yyyy' | 'm' | 'mm' | 'd' | 'dd' ; type DateOrString = Date | string ; export default { firstDayOfMonth ( date ) { const d = date . constructor === Date ? date : this . cloneDate ( date , 'dd' , 1 ) ; return d . getDay ( ) ; } , getDaysInMonth ( year , month ) { return new Date ( year , month , 0 ) . getDate ( ) ; } , getDaysByDate ( date ) { const tmp = this . parseDate ( date ) ; return new Date ( tmp . getFullYear ( ) , tmp . getMonth ( ) + 1 , 0 ) . getDate ( ) ; } , getCurrMonthInfo ( year , month ) { return { dayCount : new Date ( year , month + 1 , 0 ) . getDate ( ) , firstDay : new Date ( year , month , 1 ) . getDay ( ) , } ; } , isLeapYear ( year ) { if ( year === '' || ! Number . isSafeInteger ( + year ) ) { throw new Error ( '年份格式不正确' ) ; } year = + year ; if ( year < 1790 ) { throw new Error ( '年份不能低于1790' ) ; } return ( + year % 4 === 0 && + year % 100 !== 0 ) || + year % 400 === 0 ; } , getMonthCount ( date1 , date2 ) { const tmp1 = this . parseDate ( date1 ) ; const tmp2 = this . parseDate ( date2 ) ; const dur = ( tmp2 . getFullYear ( ) - tmp1 . getFullYear ( ) ) * 12 + ( tmp2 . getMonth ( ) - tmp1 . getMonth ( ) ) ; return Math . abs ( dur ) + 1 ; } , isToday ( date ) { return this . isOneDay ( date , new Date ( ) ) ; } , isOneDay ( date1 , date2 ) { if ( ! date1 || ! date2 ) { return false ; } const tmp1 = this . parseDate ( date1 ) ; const tmp2 = this . parseDate ( date2 ) ; return tmp1 . toDateString ( ) === tmp2 . toDateString ( ) ; } , isOneMonth ( date1 , date2 ) { if ( ! date1 || ! date2 ) { return false ; } const tmp1 = this . parseDate ( date1 ) ; const tmp2 = this . parseDate ( date2 ) ; return tmp1 . getFullYear ( ) === tmp2 . getFullYear ( ) && tmp1 . getMonth ( ) === tmp2 . getMonth ( ) ; } , getDay ( date , opt ? ) { const realDate = this . cloneDate ( date ) ; let array = [ '周日' , '周一' , '周二' , '周三' , '周四' , '周五' , '周六' ] ; if ( opt && opt instanceof Array ) { array = opt ; } return array [ realDate . getDay ( ) ] ; } , parseDay ( date ) { const tmp = this . parseDate ( date ) ; return new Date ( tmp . getFullYear ( ) , tmp . getMonth ( ) , tmp . getDate ( ) ) ; } , parseDate ( date ) { if ( date . constructor === Date ) { return date ; } if ( date . constructor === String ) { if ( + date ) { return new Date ( + date ) ; } date = date . replace ( /-/gi , '/' ) ; } return new Date ( date ) ; } , cloneDate ( date , type ? , during ? ) { const tmp = new Date ( this . parseDate ( date ) ) ; if ( ! type || ! during ) { return tmp ; } switch ( type ) { case 'y' : tmp . setFullYear ( tmp . getFullYear ( ) + during ) ; break ; case 'yyyy' : tmp . setFullYear ( during ) ; break ; case 'm' : tmp . setMonth ( tmp . getMonth ( ) + during ) ; break ; case 'mm' : tmp . setMonth ( during ) ; break ; case 'd' : tmp . setDate ( tmp . getDate ( ) + during ) ; break ; case 'dd' : tmp . setDate ( during ) ; break ; default : break ; } return tmp ; } , } ;