54 lines
1.2 KiB
JavaScript
54 lines
1.2 KiB
JavaScript
import dayjs from "dayjs"; // 导入日期js
|
||
|
||
// 以下是时间计算类函数 ------------------------------------------------------时间计算---------------------------------------
|
||
/**
|
||
* 计算时差
|
||
* startDate:开始时间
|
||
* endDate:结束时间
|
||
* unit:单位 days、months、yesrs
|
||
*/
|
||
export function timeDiffTime(startDate, endDate, unit = "days") {
|
||
return dayjs(endDate).diff(startDate, unit);
|
||
}
|
||
|
||
/**
|
||
* 比较时间,是否之前
|
||
* startDate:开始时间
|
||
* endDate:结束时间
|
||
* unit:单位 days、months、yesrs
|
||
*/
|
||
export function timeIsBefore(startDate, endDate, unit = "days") {
|
||
return dayjs(startDate).isBefore(endDate, unit);
|
||
}
|
||
|
||
|
||
/**
|
||
* 时间加计算函数
|
||
* date:原时间
|
||
* num:需要增加的时间数量
|
||
* nuit:增加时间的单位 day year
|
||
*/
|
||
export function timeAdd(date, num = 1, nuit = "day", format = "YYYY-MM-DD") {
|
||
return dayjs(date)
|
||
.add(num, nuit)
|
||
.format(format);
|
||
}
|
||
|
||
|
||
/**
|
||
* 时间格式化函数
|
||
* date 需要格式化的数据
|
||
* format 格式化的格式
|
||
*/
|
||
export function timeFormat(date, format = "YYYY-MM-DD") {
|
||
return dayjs(date).format(format);
|
||
}
|
||
|
||
|
||
/**
|
||
* 查询时间是周几
|
||
*/
|
||
export function timeInWeek(date) {
|
||
return dayjs(date).day();
|
||
}
|