import dayjs from 'dayjs' ; import weekOfYear from 'dayjs/plugin/weekOfYear' ; import IsoWeek from 'dayjs/plugin/isoWeek' ; import { Variables } from '@/constants/vars' ; import { HeaderDateOptions , HeaderDateUnit } from '@/typings/ParamOptions' ; /** ********************************** */ /** *** 下面方法全部使用 dayjs 实现 **** */ /** ********************************** */ dayjs . extend ( weekOfYear ) ; dayjs . extend ( IsoWeek ) ; param date export function createDate ( date ? ) { return dayjs ( date ) . toDate ( ) ; } param date export function isValidDate ( date ) { return dayjs ( date ) . isValid ( ) ; } export function getWeekStr ( date ) { return `第 ${ dayjs ( date ) . week ( ) } 周` ; } export function getMonthStr ( date ) { return `${ dayjs ( date ) . month ( ) + 1 } 月` ; } export function getDayStr ( date ) { return dayjs ( date ) . date ( ) . toString ( ) ; } param startDate param endDate param format export function dateList ( startDate , endDate , unit ) { const r = { } as { [ key ] : { len : number ; text : string ; } [ ] ; } ; const startTime = createDate ( startDate ) . getTime ( ) ; const endTime = createDate ( endDate ) . getTime ( ) ; for ( let i = startTime ; i <= endTime ; ) { const year = dayjs ( i ) . year ( ) ; const month = `${ dayjs ( i ) . month ( ) + 1 }` . padStart ( 2 , '0' ) ; switch ( unit ) { case 'week' : if ( r [ `${ year }-${ month }` ] ) { r [ `${ year }-${ month }` ] . push ( { text : getWeekStr ( i ) , len : 7 } ) ; } else { r [ `${ year }-${ month }` ] = [ { text : getWeekStr ( i ) , len : i === startTime ? 7 - dayjs ( i ) . day ( ) : 7 } ] ; } i += i === startTime ? ( 7 - dayjs ( i ) . day ( ) ) * Variables . time . millisecondOfDay : Variables . time . millisecondOfWeek ; break ; case 'month' : if ( r [ `${ year }` ] ) { r [ `${ year }` ] . push ( { text : getMonthStr ( i ) , len : dayjs ( i ) . daysInMonth ( ) } ) ; } else { r [ `${ year }` ] = [ { text : getMonthStr ( i ) , len : i === startTime ? dayjs ( i ) . daysInMonth ( ) - dayjs ( i ) . date ( ) + 1 : dayjs ( i ) . daysInMonth ( ) } ] ; } i += i === startTime ? ( dayjs ( i ) . daysInMonth ( ) - dayjs ( i ) . date ( ) + 1 ) * Variables . time . millisecondOfDay : dayjs ( i ) . daysInMonth ( ) * Variables . time . millisecondOfDay ; break ; case 'day' : default : if ( r [ `${ year }-${ month }` ] ) { r [ `${ year }-${ month }` ] . push ( { text : getDayStr ( i ) , len : 1 } ) ; } else { r [ `${ year }-${ month }` ] = [ { text : getDayStr ( i ) , len : 1 } ] ; } i += Variables . time . millisecondOfDay ; } } const res = [ ] as HeaderDateOptions [ ] ; for ( const key in r ) { res . push ( { unit : key , one : r [ key ] } ) ; } return res ; } param startDate param endDate export function getDateInterval ( startDate , endDate ) { return createDate ( endDate ) . getTime ( ) - createDate ( startDate ) . getTime ( ) ; } param date1 param date2 export function compareDate ( date1 , date2 ) { const l = createDate ( date1 ) . getTime ( ) ; const r = createDate ( date2 ) . getTime ( ) ; if ( l < r ) return 'l' ; if ( l > r ) return 'r' ; return 'e' ; } /** ********************************** */ /** ********************************** */ param date param offset export function getDateOffset ( date , offset ) { const currentTime = createDate ( date ) . getTime ( ) + offset ; return createDate ( currentTime ) . getTime ( ) ; } param date1 param date2 export function sameDate ( date1 , date2 ) { const d1 = createDate ( date1 ) ; const d2 = createDate ( date2 ) ; if ( d1 . getFullYear ( ) === d2 . getFullYear ( ) && d1 . getMonth ( ) === d2 . getMonth ( ) && d1 . getDate ( ) === d2 . getDate ( ) ) return true ; return false ; } param date export function checkDate ( date ) { if ( typeof date === 'string' || typeof date === 'number' ) { try { date = createDate ( date ) ; } catch ( error ) { return null ; } } return date ; } type FormatKey = 'M+' | 'd+' | 'h+' | 'H+' | 'm+' | 's+' | 'q+' | 'S' ; export type LanguageKey = 'zh' | 'en' ; param date param fmt param lang return export function formatDate ( date , fmt = 'yyyy-MM-dd' , lang = 'zh' ) { const WEEK = { zh : [ '星期日' , '星期一' , '星期二' , '星期三' , '星期四' , '星期五' , '星期六' ] , en : [ 'Sunday' , 'Monday' , 'Tuesday' , 'Wednesday' , 'Thursday' , 'Friday' , 'Saturday' ] } ; if ( [ 'zh' , 'en' ] . indexOf ( lang ) === - 1 ) lang = 'zh' ; date = createDate ( date ) ; const o = { 'M+' : date . getMonth ( ) + 1 , 'd+' : date . getDate ( ) , 'h+' : date . getHours ( ) % 12 === 0 ? 12 : date . getHours ( ) % 12 , 'H+' : date . getHours ( ) , 'm+' : date . getMinutes ( ) , 's+' : date . getSeconds ( ) , 'q+' : Math . floor ( ( date . getMonth ( ) + 3 ) / 3 ) , S : date . getMilliseconds ( ) } ; let k ; for ( k in o ) if ( new RegExp ( `(${ k })` ) . test ( fmt ) ) { const v = o [ k ] . toString ( ) ; fmt = fmt . replace ( RegExp . $1 , RegExp . $1 . length === 1 ? v : `00${ v }` . substr ( `${ v }` . length ) ) ; } if ( /(y+)/ . test ( fmt ) ) fmt = fmt . replace ( RegExp . $1 , `${ date . getFullYear ( ) }` . substr ( 4 - RegExp . $1 . length ) ) ; if ( /(D+)/ . test ( fmt ) ) { fmt = fmt . replace ( RegExp . $1 , WEEK [ lang ] [ date . getDay ( ) ] ) ; } return fmt ; } export function getMillisecond ( unit ) { switch ( unit ) { case 'week' : case 'month' : case 'day' : default : return Variables . time . millisecondOfDay ; } }