简介
npm install dayjs --save
常用的函数
解析特定格式的日期
import customParseFormat from 'dayjs/plugin/customParseFormat';
dayjs.extend(customParseFormat)
dayjs("12-25-1995", "MM-DD-YYYY")
取值/赋值
- 默认
dayjs()获取的是当前的时间,可以用toDate方法将dayjs格式改为Date对象
// dayjs.toDate()=new Date()
dayjs.toDate()
dayjs().year()
dayjs().year(2000)
dayjs('2021/11/20').year()
import quarterOfYear from 'dayjs/plugin/quarterOfYear';
dayjs.extend(quarterOfYear)
dayjs('2010-04-01').quarter()
dayjs('2010-04-01').quarter(2)
操作
dayjs().add(7, 'day')
dayjs().startOf('year')
dayjs().endOf('month')
dayjs().startOf('date')
显示
- 格式化,具体DD,MM,YYYY代表啥可以去官网看下
dayjs('2019-01-25').format('DD/MM/YYYY')
- 获取两个时间之间的时间差,
diff可传三个参数
只传第一个参数出来的是时间戳差
传第二个参数代表求的是月差日差还是啥差
第三个参数可传可不传,传true代表加浮点数,比较精确
const date1 = dayjs('2019-01-25')
const date2 = dayjs('2018-06-05')
date1.diff(date2)
date1.diff(date2,'month')
date1.diff(date2, 'month', true)
查询
- 可查询是否为某个时间之前,之后,是否相同,是否是两个时间之间,是否为闰年等等