Day.js installation and use, and records of common knowledge points

* Day.js official website

Day.js Chinese website
Day.js is an easy-to-use date js library.
The file size is only about 2KB. Less JavaScript is downloaded, parsed and executed, leaving more time for the code.
Contains a large number of date processing methods, which are of great help in our usual date processing!
This blog only records some common methods, and the complete method can be found on the official website

dayjs installation and introduction

// 安装
npm i dayjs

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

* Get Time

// 获取当前时间
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') // 星期几

* Get a custom time interval (increase and decrease the current time)

Support chain call

// 增加
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

insert image description here

* Get the difference between two times

Returns the difference between two datetimes in the specified unit.
The default unit is milliseconds
. To get the difference in other units, pass in the corresponding unit in the second parameter .
By default dayjs#diff will round the result to an integer. If you want to get a floating point number, pass true as the third parameter .

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

Relative Time

Returns the relative time from now to the current instance. This feature relies on the RelativeTime plugin

relative to current time

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 年

relative specified time

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 年

date size comparison

whether before

dayjs().isBefore()

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

after

dayjs().isAfter()

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

Is it the same

dayjs().isSame()

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

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

Maximum, Minimum

Depends on the MinMax plugin

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')])

check

loose validation

Only check if the incoming value can be parsed into a time and date

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

Strict validation

Check if the passed in value can be parsed and is a meaningful date. The last two parameters format and strict must be provided.

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

Guess you like

Origin blog.csdn.net/IT_dabai/article/details/129356041