dayjs 是一个常用的时间操作库。在这里记录一下常用的几个api
解析
dayjs ( )
dayjs ( '2020-02-02' )
dayjs ( "12-25-1995" , "MM-DD-YYYY" )
dayjs ( new Date ( 2018 , 8 , 18 ) )
加减时间
dayjs ( ) . add ( 7 , 'day' )
dayjs ( ) . subtract ( 7 , 'year' )
时间格式化
dayjs ( ) . format ( )
dayjs ( '2019-01-25' ) . format ( '[YYYYescape] YYYY-MM-DDTHH:mm:ssZ[Z]' )
dayjs ( '2019-01-25' ) . format ( 'DD/MM/YYYY' )
Format Output Description YY 18 Two-digit year YYYY 2018 Four-digit year M 1-12 The month, beginning at 1 MM 01-12 The month, 2-digits MMM Jan-Dec The abbreviated month name MMMM January-December The full month name D 1-31 The day of the month DD 01-31 The day of the month, 2-digits d 0-6 The day of the week, with Sunday as 0 dd Su-Sa The min name of the day of the week ddd Sun-Sat The short name of the day of the week dddd Sunday-Saturday The name of the day of the week H 0-23 The hour HH 00-23 The hour, 2-digits h 1-12 The hour, 12-hour clock hh 01-12 The hour, 12-hour clock, 2-digits m 0-59 The minute mm 00-59 The minute, 2-digits s 0-59 The second ss 00-59 The second, 2-digits SSS 000-999 The millisecond, 3-digits Z +05:00 The offset from UTC ZZ +0500 The offset from UTC, 2-digits A AM PM a am pm … … Other formats ( dependent AdvancedFormat plugin )
时间比较
dayjs ( ) . isBefore ( dayjs ( '2011-01-01' ) )
dayjs ( ) . isBefore ( '2011-01-01' , 'year' )
dayjs ( ) . isSame ( '2011-01-01' , 'year' )
dayjs ( ) . isAfter ( '2011-01-01' , 'year' )
dayjs. extend ( isSameOrBefore)
dayjs ( ) . isSameOrBefore ( '2011-01-01' , 'year' )
dayjs. extend ( isSameOrAfter)
dayjs ( ) . isSameOrAfter ( '2011-01-01' , 'year' )
dayjs. extend ( isBetween)
dayjs ( '2010-10-20' ) . isBetween ( '2010-10-19' , dayjs ( '2010-10-25' ) )
dayjs. extend ( isLeapYear)
dayjs ( '2000-01-01' ) . isLeapYear ( )
时间相减
const date1 = dayjs ( '2019-01-25' )
const date2 = dayjs ( '2018-06-05' )
date1. diff ( date2)
date1. diff ( '2018-06-05' , 'month' )
单位
Unit Shorthand Description date D Date of Month day d Day of Week (Sunday as 0, Saturday as 6) week w Week of Year month M Month (January as 0, December as 11) quarter Q Quarter ( dependent QuarterOfYear plugin ) year y Year hour h Hour minute m Minute second s Second millisecond ms Millisecond