dayjs常用操作

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() 
// current date in ISO8601, without fraction seconds e.g. '2020-04-02T08:02:17-05:00'

dayjs('2019-01-25').format('[YYYYescape] YYYY-MM-DDTHH:mm:ssZ[Z]') 
// 'YYYYescape 2019-01-25T00:00:00-02:00Z'

dayjs('2019-01-25').format('DD/MM/YYYY') // '25/01/2019'
FormatOutputDescription
YY18Two-digit year
YYYY2018Four-digit year
M1-12The month, beginning at 1
MM01-12The month, 2-digits
MMMJan-DecThe abbreviated month name
MMMMJanuary-DecemberThe full month name
D1-31The day of the month
DD01-31The day of the month, 2-digits
d0-6The day of the week, with Sunday as 0
ddSu-SaThe min name of the day of the week
dddSun-SatThe short name of the day of the week
ddddSunday-SaturdayThe name of the day of the week
H0-23The hour
HH00-23The hour, 2-digits
h1-12The hour, 12-hour clock
hh01-12The hour, 12-hour clock, 2-digits
m0-59The minute
mm00-59The minute, 2-digits
s0-59The second
ss00-59The second, 2-digits
SSS000-999The millisecond, 3-digits
Z+05:00The offset from UTC
ZZ+0500The offset from UTC, 2-digits
AAM PM
aam pm
Other formats ( dependent AdvancedFormat plugin )

时间比较

dayjs().isBefore(dayjs('2011-01-01')) // default milliseconds
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) // 20214000000 default milliseconds
date1.diff('2018-06-05', 'month') // 7
单位
UnitShorthandDescription
dateDDate of Month
daydDay of Week (Sunday as 0, Saturday as 6)
weekwWeek of Year
monthMMonth (January as 0, December as 11)
quarterQQuarter ( dependent QuarterOfYear plugin )
yearyYear
hourhHour
minutemMinute
secondsSecond
millisecondmsMillisecond
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值