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();
|
|
|
|
|
|
}
|
2025-07-09 03:04:53 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
*处理excel 数字转时间
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function excelDateToJSDate(excelDate) {
|
|
|
|
|
|
// Excel把1900年1月1日存储为数字1
|
|
|
|
|
|
const date = new Date(1900, 0, excelDate - 1);
|
|
|
|
|
|
// 时间格式化为 YYYY-MM-DD HH:mm:ss
|
|
|
|
|
|
const year = date.getFullYear();
|
|
|
|
|
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
|
|
|
|
const day = String(date.getDate()).padStart(2, '0');
|
|
|
|
|
|
const hours = String(date.getHours()).padStart(2, '0');
|
|
|
|
|
|
const minutes = String(date.getMinutes()).padStart(2, '0');
|
|
|
|
|
|
const seconds = String(date.getSeconds()).padStart(2, '0');
|
|
|
|
|
|
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
|
|
|
|
|
|
}
|