gaoluyang
2026-06-24 c0cb161bb52ce0fbdce5c66ec391d107c75e2452
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import dayjs from 'dayjs';
import timezone from 'dayjs/plugin/timezone.js';
import utc from 'dayjs/plugin/utc.js';
 
dayjs.extend(utc);
dayjs.extend(timezone);
 
type FormatDate = Date | dayjs.Dayjs | number | string;
 
type Format =
  | 'HH'
  | 'HH:mm'
  | 'HH:mm:ss'
  | 'YYYY'
  | 'YYYY-MM'
  | 'YYYY-MM-DD'
  | 'YYYY-MM-DD HH'
  | 'YYYY-MM-DD HH:mm'
  | 'YYYY-MM-DD HH:mm:ss'
  | (string & {});
 
export function formatDate(time?: FormatDate, format: Format = 'YYYY-MM-DD') {
  // 日期不存在,则返回空
  if (time === undefined || time === null || time === '') {
    return '';
  }
  try {
    const date = dayjs.isDayjs(time) ? time : dayjs(time);
    if (!date.isValid()) {
      throw new Error('Invalid date');
    }
    return date.tz().format(format);
  } catch (error) {
    console.error(`Error formatting date: ${error}`);
    return String(time ?? '');
  }
}
 
export function formatDateTime(time?: FormatDate) {
  return formatDate(time, 'YYYY-MM-DD HH:mm:ss');
}
 
export function formatDate2(date: Date, format?: string): string {
  // 日期不存在,则返回空
  if (!date) {
    return '';
  }
  // 日期存在,则进行格式化
  return date ? dayjs(date).format(format ?? 'YYYY-MM-DD HH:mm:ss') : '';
}
 
export function isDate(value: any): value is Date {
  return value instanceof Date;
}
 
export function isDayjsObject(value: any): value is dayjs.Dayjs {
  return dayjs.isDayjs(value);
}
 
/**
 * element plus 的时间 Formatter 实现,使用 YYYY-MM-DD HH:mm:ss 格式
 *
 * @param _row
 * @param _column
 * @param cellValue 字段值
 */
export function dateFormatter(_row: any, _column: any, cellValue: any): string {
  return cellValue ? formatDate(cellValue)?.toString() || '' : '';
}
 
/**
 * 获取当前时区
 * @returns 当前时区
 */
export const getSystemTimezone = () => {
  return dayjs.tz.guess();
};
 
/**
 * 自定义设置的时区
 */
let currentTimezone = getSystemTimezone();
 
/**
 * 设置默认时区
 * @param timezone
 */
export const setCurrentTimezone = (timezone?: string) => {
  currentTimezone = timezone || getSystemTimezone();
  dayjs.tz.setDefault(currentTimezone);
};
 
/**
 * 获取设置的时区
 * @returns 设置的时区
 */
export const getCurrentTimezone = () => {
  return currentTimezone;
};
 
/**
 * 把 antd TimePicker / DatePicker `@update:value` 回传的值统一成字符串。
 *
 * antd 在设置了 `value-format` 后实际只会回传字符串,
 * 但 `@update:value` 的类型仍包含 `Dayjs`,调用方需要做一次类型归一。
 *
 * - 空值(null / undefined / '' / 0)返回 ''
 * - 已经是字符串:原样返回(保持 `value-format` 已格式化的结果)
 * - 兜底的 Dayjs:调用 `.format()` 转默认 ISO 字符串
 */
export function formatDayjs(
  value: dayjs.Dayjs | null | string | undefined,
): string {
  if (!value) {
    return '';
  }
  return typeof value === 'string' ? value : value.format();
}