import { addDays , endOfDay , endOfMonth , endOfWeek , endOfYear , parse , parseISO , startOfDay , startOfMonth , startOfWeek , startOfYear , subMonths , subWeeks , subYears } from 'date-fns' ; import type { NzSafeAny } from 'ng-zorro-antd/core/types' ; param type param time export function getTimeDistance ( type , time ? ) { time = time ? typeof time === 'string' ? parse ( time , 'yyyy-MM-dd HH:mm:ss' , new Date ( ) ) : new Date ( time ) : new Date ( ) ; const options = { weekStartsOn : 1 } ; let res ; switch ( type ) { case 'today' : res = [ time , time ] ; break ; case '-today' : res = [ addDays ( time , - 1 ) , time ] ; break ; case 'yesterday' : res = [ addDays ( time , - 1 ) , addDays ( time , - 1 ) ] ; break ; case 'week' : res = [ startOfWeek ( time , options ) , endOfWeek ( time , options ) ] ; break ; case '-week' : res = [ startOfWeek ( subWeeks ( time , 1 ) , options ) , endOfWeek ( subWeeks ( time , 1 ) , options ) ] ; break ; case 'month' : res = [ startOfMonth ( time ) , endOfMonth ( time ) ] ; break ; case '-month' : res = [ startOfMonth ( subMonths ( time , 1 ) ) , endOfMonth ( subMonths ( time , 1 ) ) ] ; break ; case 'year' : res = [ startOfYear ( time ) , endOfYear ( time ) ] ; break ; case '-year' : res = [ startOfYear ( subYears ( time , 1 ) ) , endOfYear ( subYears ( time , 1 ) ) ] ; break ; default : res = type > 0 ? [ time , addDays ( time , type ) ] : [ addDays ( time , type ) , time ] ; break ; } return fixEndTimeOfRange ( res ) ; } /**
 * fix time is the most, big value
 */ export function fixEndTimeOfRange ( dates ) { return [ startOfDay ( dates [ 0 ] ) , endOfDay ( dates [ 1 ] ) ] ; } export type ToDateOptions = string | { formatString ? : string ; defaultValue ? : NzSafeAny ; } ; param formatString param defaultValue export function toDate ( value , options ? ) { if ( typeof options === 'string' ) options = { formatString : options } ; const { formatString , defaultValue } = { formatString : 'yyyy-MM-dd HH:mm:ss' , defaultValue : new Date ( NaN ) , ... options } ; if ( value == null ) { return defaultValue ; } if ( value instanceof Date ) { return value ; } if ( typeof value === 'number' || ( typeof value === 'string' && /[0-9]{10,13}/ . test ( value ) ) ) { return new Date ( + value ) ; } let tryDate = parseISO ( value ) ; if ( isNaN ( tryDate as NzSafeAny ) ) { tryDate = parse ( value , formatString ! , new Date ( ) ) ; } return isNaN ( tryDate as NzSafeAny ) ? defaultValue : tryDate ; }