2024-05-27 16:51:04 +00:00
|
|
|
|
import dayjs from "dayjs"; // 导入日期js
|
|
|
|
|
|
|
|
|
|
|
|
// 以下是时间计算类函数 ------------------------------------------------------时间计算---------------------------------------
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 计算时差
|
|
|
|
|
|
* startDate:开始时间
|
|
|
|
|
|
* endDate:结束时间
|
|
|
|
|
|
* unit:单位 days、months、yesrs
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function timeDiffTime(startDate, endDate, unit = "days") {
|
2024-08-16 22:13:34 +00:00
|
|
|
|
|
|
|
|
|
|
if(unit=="days"){
|
|
|
|
|
|
endDate=timeFormatDay(endDate);
|
|
|
|
|
|
startDate=timeFormatDay(startDate);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-27 16:51:04 +00:00
|
|
|
|
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 格式化的格式
|
|
|
|
|
|
*/
|
2024-06-08 13:10:56 +00:00
|
|
|
|
export function timeFormat(date, format = "YYYY-MM-DD HH:mm:ss") {
|
|
|
|
|
|
return dayjs(date).format(format);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function timeFormatDay(date, format = "YYYY-MM-DD") {
|
2024-05-27 16:51:04 +00:00
|
|
|
|
return dayjs(date).format(format);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 查询时间是周几
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function timeInWeek(date) {
|
|
|
|
|
|
return dayjs(date).day();
|
|
|
|
|
|
}
|