guoshilong
2023-11-13 54849757852f6ab40eb17afbd03d1d839b60a38d
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
import {
  DATE_FORMAT,
  DEFAULT_PLACEHOLDER
} from '/@/utils/constants'
import moment, { Moment } from 'moment'
 
// 时间字符串 或者 Unix 时间戳(毫秒数)
export function formatDateTime (time: string | number, format = DATE_FORMAT) {
  return time ? moment(time, format) : DEFAULT_PLACEHOLDER
}
 
// Unix 时间戳 (秒)
export function formatUnixTime (time: number, format = DATE_FORMAT): string {
  return time ? moment.unix(time).format(format) : DEFAULT_PLACEHOLDER
}
 
export function dateFormat (date:any, format:string) {
  format = format || 'yyyy-MM-dd hh:mm:ss'
  if (date !== 'Invalid Date') {
    const o = {
      'M+': date.getMonth() + 1, // month
      'd+': date.getDate(), // day
      'h+': date.getHours(), // hour
      'm+': date.getMinutes(), // minute
      's+': date.getSeconds(), // second
      'q+': Math.floor((date.getMonth() + 3) / 3), // quarter
      S: date.getMilliseconds() // millisecond
    }
    if (/(y+)/.test(format)) {
      format = format.replace(RegExp.$1,
        (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    for (const k in o) {
      if (new RegExp('(' + k + ')').test(format)) {
        format = format.replace(RegExp.$1,
          RegExp.$1.length === 1
            ? o[k]
            : ('00' + o[k]).substr(('' + o[k]).length))
      }
    }
    return format
  }
  return ''
}
 
export function convertTimestampToDate (timestamp:any, format?:string) {
  if (timestamp && (typeof timestamp === 'string') && timestamp.indexOf('-') > -1) {
    // eslint-disable-next-line no-useless-escape
    timestamp = timestamp.replace(/\-/g, '/')
  }
  const time = new Date(timestamp)
  const formatterTime = dateFormat(time, format)
  return formatterTime
}
 
// 返回年月日
export const timestampToTime = (timestamp:number) => {
  if (!timestamp) return
  // 时间戳为10位需*1000,时间戳为13位不需乘1000
  const date = new Date(timestamp)
  const Y = date.getFullYear() + '-'
  const M =
      (date.getMonth() + 1 < 10
        ? '0' + (date.getMonth() + 1)
        : date.getMonth() + 1) + '-'
  const D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate())
  return Y + M + D
}
 
// 获取时间戳对应的日期
export const getDateFromTimestamp = (timestamp:number) => {
  const date = new Date(timestamp)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hours = date.getHours()
  const minutes = date.getMinutes()
  const seconds = date.getSeconds()
  return { year, month, day, hours, minutes, seconds }
}
 
// 将日期设置为今天的日期,返回时间戳
export const setTodayTime = (dateObj:any) => {
  const today = new Date()
  const year = today.getFullYear()
  const month = today.getMonth()
  const day = today.getDate()
  const { hours, minutes, seconds } = dateObj
  return new Date(year, month, day, hours, minutes, seconds).getTime()
}
 
// 判断任务状态是否是当天进行对比
export const isToday = (timestamp:number) => {
  const { hours, minutes, seconds } = getDateFromTimestamp(timestamp)
  const todayTaskTimestamp = setTodayTime({ hours, minutes, seconds })
  // 判断传过来的时间戳是否是今天任务时间时间戳
  if (timestamp === todayTaskTimestamp) {
    return true
  } else if (todayTaskTimestamp < timestamp) {
    return false
  } else {
    const oneDayTimestamp = 24 * 60 * 60 * 1000
    timestamp += oneDayTimestamp
    isToday(timestamp)
  }
}