All files / src/date dateFormat.ts

92.31% Statements 12/13
75% Branches 3/4
100% Functions 2/2
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 342x 2x 6x     6x 3x   6x 6x                               6x     6x 90x   6x    
import { isDate } from './isDate';
export const dateFormat = (date: string | Date | number, format: string) => {
  Iif (!isDate(date)) return null;
  // 时间格式化函数
  let time: Date | number;
  if (!isNaN(date as any)) {
    date = Number(date);
  }
  time = new Date(date);
  const o: Record<string, any> = {
    YYYY: time.getFullYear(),
    YY: `${time.getFullYear()}`.slice(-2),
    MM: `0${time.getMonth() + 1}`.slice(-2),
    M: time.getMonth() + 1,
    DD: `0${time.getDate()}`.slice(-2),
    D: time.getDate(),
    HH: `0${time.getHours()}`.slice(-2),
    H: time.getHours(),
    hh: `0${time.getHours() % 12}`.slice(-2),
    h: time.getHours() % 12,
    mm: `0${time.getMinutes()}`.slice(-2),
    m: time.getMinutes(),
    ss: `0${time.getSeconds()}`.slice(-2),
    s: time.getSeconds(),
    w: (function () {
      return ['日', '一', '二', '三', '四', '五', '六'][time.getDay()];
    })(),
  };
  for (const k in o) {
    format = format.replace(k, o[k]);
  }
  return format;
};