tianji/src/shared/date.ts

21 lines
552 B
TypeScript
Raw Normal View History

2023-10-08 10:51:47 +00:00
import dayjs, { ConfigType } from 'dayjs';
export type DateUnit = 'minute' | 'hour' | 'day' | 'month' | 'year';
export function getMinimumUnit(
startDate: ConfigType,
endDate: ConfigType
): DateUnit {
2023-10-08 10:51:47 +00:00
if (dayjs(endDate).diff(startDate, 'minutes') <= 60) {
return 'minute';
} else if (dayjs(endDate).diff(startDate, 'hours') <= 48) {
return 'hour';
} else if (dayjs(endDate).diff(startDate, 'days') <= 90) {
return 'day';
} else if (dayjs(endDate).diff(startDate, 'months') <= 24) {
return 'month';
}
return 'year';
}