Ben Lin
2024-08-22 436b52186129e60ba72c20e43d2845bc3f899901
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
/**
 * 转换时间格式
 * @param date 将转换的时间
 * @param fmt 转换成的格式 yyyy-MM-dd|yyyy-MM-dd hh:mm:ss
 */
export const DateFormat = (date: string, fmt: string): string => {
  if (date && fmt) {
    const _date = new Date(date);
    const o = {
      'M+': _date.getMonth() + 1, //月份
      'd+': _date.getDate(), //日
      'h+': _date.getHours(), //小时
      'm+': _date.getMinutes(), //分
      's+': _date.getSeconds(), //秒
      'q+': Math.floor((_date.getMonth() + 3) / 3), //季度
      S: _date.getMilliseconds(), //毫秒
    };
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (_date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (const k in o) {
      if (new RegExp('(' + k + ')').test(fmt)) {
        fmt = fmt.replace(
          RegExp.$1,
          RegExp.$1.length == 1
            ? (o as any)[k]
            : ('00' + (o as any)[k]).substr(('' + (o as any)[k]).length),
        );
      }
    }
    return fmt;
  } else {
    return '';
  }
};
 
// const newDate = '2022-09-06T15:48:27.239+08:00';
// const D1 = DateFormat(newDate, 'yyyy-MM-dd');
// const D2 = DateFormat(newDate, 'yyyy-MM-dd hh:mm:ss');
// console.log(D1, D2);