Day.js 安装使用,以及常用知识点记录

* Day.js 官网

Day.js中文网
Day.js 好用的日期 js 库
文件大小只有2KB左右,下载、解析和执行的JavaScript更少,为代码留下更多的时间。
包含大量对日期的处理方法,在我们平时的日期处理上,给予了很大的帮助!
本篇博客只记录了一些常用方法,完整的方法,可前往官网查阅

dayjs 安装、引入

// 安装
npm i dayjs

// 页面文件引入
import dayjs from 'dayjs';

* 获取时间

// 获取当前时间
dayjs().format('YYYY-MM-DD');  // 2023-03-06
dayjs().format('YYYY-MM-DD HH:mm:ss'); // 2023-03-06 13:47:12

// 获取本日时间段
dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss')
dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss')
// 获取本周时间段
dayjs().startOf('week').add(1, 'day').format('YYYY-MM-DD HH:mm:ss')
dayjs().endOf('week').add(1, 'day').format('YYYY-MM-DD HH:mm:ss')
// 获取本月时间段
dayjs().startOf('month').format('YYYY-MM-DD HH:mm:ss')
dayjs().endOf('month').format('YYYY-MM-DD HH:mm:ss')
// 传入时间戳(数字),获取对应时间
dayjs(1318781876406).format('YYYY-MM-DD HH:mm:ss');   // 2011-10-17 00:17:56
// 传入数组,获取对应时间
dayjs([2010, 6, 10]).format('YYYY-MM-DD HH:mm:ss');  // 2010-06-10 00:00:00


// 调用 dayjs#toDate 从 Day.js 对象中获取原生的 Date 对象
dayjs('2019-01-25').toDate()
// 是否是Day.js
// 这表示一个变量是否为 Day.js 对象。
dayjs.isDayjs(dayjs()) // true
dayjs.isDayjs(new Date()) // false

// 获取年、月、日、小时、分钟、秒、毫秒
dayjs().year();
dayjs().month();
dayjs().date();
dayjs().hour();
dayjs().minute();
dayjs().second();
dayjs().millisecond();
dayjs().day(); // 获取当前是星期几
dayjs('2019-01-25').daysInMonth() // 31  获取当前月份包含的天数



// 另一种方法获取年、月、日、小时、分钟、秒、毫秒、星期几
//dayjs().get('year')
//dayjs().get('month') // start 0
//dayjs().get('date')
//dayjs().get('hour')
//dayjs().get('minute')
//dayjs().get('second')
//dayjs().get('millisecond')
//dayjs().get('day') // 星期几

* 获取自定时间区间(对当前时间进行增加、减少)

支持链式调用

// 增加
dayjs().add(7, 'day') // 在当前时间上增加7天
dayjs().add(7, 'day').format('YYYY-MM-DD HH:mm:ss'); // 2023-03-13 10:39:51
// 减少
dayjs().subtract(7, 'year') // 在当前时间上减少7年
dayjs().subtract(7, 'year').format('YYYY-MM-DD HH:mm:ss'); // 2016-03-06 10:39:05

在这里插入图片描述

* 获取两个时间的差值

返回指定单位下两个日期时间之间的差异
默认单位是毫秒
要获取其他单位下的差异,则在第二个参数传入相应的单位。
默认情况下 dayjs#diff 会将结果进位成整数。 如果要得到一个浮点数,将 true 作为第三个参数传入。

const date1 = dayjs('2019-01-25')
const date2 = dayjs('2018-06-05')
date1.diff(date2) // 20214000000 默认单位是毫秒

const date3 = dayjs('2019-01-25')
date3.diff('2018-06-05', 'month') // 7

const date4 = dayjs('2019-01-25')
date4.diff('2018-06-05', 'month', true) // 7.645161290322581

相对时间

返回现在到当前实例的相对时间。此功能依赖 RelativeTime 插件

相对当前时间

dayjs.extend(relativeTime)
dayjs('1999-01-01').fromNow() // xx 年前
dayjs('1999-01-01').toNow() // xx 年后

// 如果传入 true,则可以获得不带后缀的值。
dayjs('1999-01-01').fromNow(true) // xx 年
dayjs('1999-01-01').toNow(true) // xx 年

相对指定时间

const a = dayjs('2000-01-01')
dayjs('1999-01-01').to(a) // 1 年后

// 如果传入 true,则可以获得不带后缀的值。
var a = dayjs('2000-01-01')
dayjs('1999-01-01').to(a, true) // 1 年

日期大小比较

是否之前

dayjs().isBefore()

dayjs().isBefore(dayjs('2011-01-01')) // 默认毫秒
dayjs().isBefore('2011-01-01', 'year') // 

是否之后

dayjs().isAfter()

dayjs().isAfter(dayjs('2011-01-01')) // 默认毫秒
dayjs().isAfter('2011-01-01', 'year')

是否相同

dayjs().isSame()

dayjs().isSame(dayjs('2011-01-01')) // 默认毫秒

// 如果想使用除了毫秒以外的单位进行比较,则将单位作为第二个参数传入。
// 当使用第二个参数时,将会连同去比较更大的单位。 如传入 month 将会比较 month 和 year。 // 传入 day 将会比较 day、 month和 year。
dayjs().isSame('2011-01-01', 'year')

最大、最小值

依赖 MinMax 插件

dayjs.extend(minMax)
// 最大值
dayjs.max(dayjs(), dayjs('2018-01-01'), dayjs('2019-01-01'))
dayjs.max([dayjs(), dayjs('2018-01-01'), dayjs('2019-01-01')])
// 最小值
dayjs.min(dayjs(), dayjs('2018-01-01'), dayjs('2019-01-01'))
dayjs.min([dayjs(), dayjs('2018-01-01'), dayjs('2019-01-01')])

校验

不严格的校验

只检查传入的值能否被解析成一个时间日期

dayjs('2022-01-33').isValid();
// true, parsed to 2022-02-02
dayjs('some invalid string').isValid();
// false

严格校验

检查传入的值能否被解析,且是否是一个有意义的日期。 最后两个参数 format 和 strict 必须提供。

dayjs('2022-02-31', 'YYYY-MM-DD', true).isValid();
// false

猜你喜欢

转载自blog.csdn.net/IT_dabai/article/details/129356041