shikeying
2023-04-07 c192f834c4e092bc7c0f2722c343c25c1be619ab
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
export function formatDate(date, fmt) {
  try {
    if (!(date instanceof Date)) {
      date = new Date(date)
    }
    if (fmt == undefined || fmt == '') {
      fmt = 'yyyy-MM-dd hh:mm:ss'
    }
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    const o = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'h+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds()
    }
    for (const k in o) {
      if (new RegExp(`(${k})`).test(fmt)) {
        const str = o[k] + ''
        fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str))
      }
    }
    return fmt
  } catch (e) {
    console.log(e)
    return '-'
  }
}
 
function padLeftZero(str) {
  return ('00' + str).substr(str.length)
}
 
/**
 * @Author wh
 * @Date 2022/2/25 - 14:35
 * @Description //日期比较
 **/
export function compare(date1, date2) {
  const dates1 = new Date(date1)
  const dates2 = new Date(date2)
  if (dates1 > dates2) {
    return true
  } else {
    return false
  }
}
 
/**
 * @Author wh
 * @Date 2022/5/13 - 15:43
 * @Description //时间格式转换
 * 传值时自动调用了toJSON()函数,手动转成你想要的字符串格式
 **/
export function FormatTime(t, date) {
  var date = new Date(date)
  var 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(t)) {
    t = t.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(t)) {
      t = t.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return t
}