Blame view

src/utils/time.ts 936 Bytes
calmound authored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import dayjs, { Dayjs } from 'dayjs';
import customParseFormat from 'dayjs/plugin/customParseFormat';

dayjs.extend(customParseFormat);

// 格式化日期
export function formatDate(
  date: Dayjs | string,
  formatString: string = 'YYYY-MM-DD',
): string {
  return dayjs(date).format(formatString);
}

// 获取当前日期
export function getCurrentDate(formatString: string = 'YYYY-MM-DD'): string {
  return dayjs().format(formatString);
}

// 计算日期差
export function calculateDateDifference(
  startDate: Dayjs | string,
  endDate: Dayjs | string,
): number {
  const start = dayjs(startDate);
  const end = dayjs(endDate);
  return end.diff(start, 'day');
}

// 自定义功能示例:判断日期是否是工作日
export function isWeekday(date: Dayjs | string): boolean {
  const dayOfWeek = dayjs(date).day();
  return dayOfWeek >= 1 && dayOfWeek <= 5; // 星期一到星期五为工作日
}

export default dayjs;