shuishen
2024-04-18 4522ab3fe8bd45ee753ef187448c1e884bbc601f
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
 
// 返回年月日
export const timestampToTime = (timestamp) => {
    if (!timestamp) return
    // 时间戳为10位需*1000,时间戳为13位不需乘1000
    var date = new Date(timestamp)
    var Y = date.getFullYear() + "-"
    var M =
        (date.getMonth() + 1 < 10
            ? "0" + (date.getMonth() + 1)
            : date.getMonth() + 1) + "-"
    var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate())
    return Y + M + D
}
 
//返回年月日 时分
export const timestampToMinute = (timestamp) => {
    if (!timestamp) return
    // 时间戳为10位需*1000,时间戳为13位不需乘1000
    var date = new Date(timestamp)
    var Y = date.getFullYear() + "-"
    var M =
        (date.getMonth() + 1 < 10
            ? "0" + (date.getMonth() + 1)
            : date.getMonth() + 1) + "-"
    var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " "
    var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ":"
    var m = (date.getMinutes() < 10
        ? "0" + (date.getMinutes())
        : date.getMinutes())
    var s = (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds())
    return Y + M + D + h + m
}
 
//返回年月日 时分秒
export const timestampToHour = (timestamp) => {
    if (!timestamp) return
    // 时间戳为10位需*1000,时间戳为13位不需乘1000
    var date = new Date(timestamp)
    var Y = date.getFullYear() + "-"
    var M =
        (date.getMonth() + 1 < 10
            ? "0" + (date.getMonth() + 1)
            : date.getMonth() + 1) + "-"
    var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " "
    var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ":"
    var m = (date.getMinutes() < 10
        ? "0" + (date.getMinutes())
        : date.getMinutes()) + ':'
    var s = (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds())
    return Y + M + D + h + m + s
}
//返回年月日时分秒
export const timeStampTosecond = (timestamp) => {
    if (!timestamp) return
    // 时间戳为10位需*1000,时间戳为13位不需乘1000
    var date = new Date(timestamp)
    var Y = date.getFullYear() + "-"
    var M =
        (date.getMonth() + 1 < 10
            ? "0" + (date.getMonth() + 1)
            : date.getMonth() + 1) + "-"
    var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " "
    var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ":"
    var m = (date.getMinutes() < 10
        ? "0" + (date.getMinutes())
        : date.getMinutes()) + ':'
    var s = (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds())
    return Y + M + D + h + "00:00"
}
 
export const getTimeBefore = (n = 8) => {
    const nowStamp = new Date().getTime()
    const beforeStamp = nowStamp - (n * 60 * 60 * 1000)
    const nowTime = timeStampTosecond(nowStamp + 60 * 1000 * 60)
    const beforeTime = timeStampTosecond(beforeStamp)
    return [beforeTime, nowTime]
}
export const Timesecend = (timestamp) => {
    if (!timestamp) return
    // 时间戳为10位需*1000,时间戳为13位不需乘1000
    var date = new Date(timestamp)
    var Y = date.getFullYear() + "-"
    var M =
        (date.getMonth() + 1 < 10
            ? "0" + (date.getMonth() + 1)
            : date.getMonth() + 1) + "-"
    var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " "
    var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ":"
    var m = (date.getMinutes() < 10
        ? "0" + (date.getMinutes())
        : date.getMinutes()) + ':'
    var s = (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds())
    return M + D + h + m + s
}
 
export const TimeMinute = (timestamp) => {
    if (!timestamp) return
    // 时间戳为10位需*1000,时间戳为13位不需乘1000
    var date = new Date(timestamp)
    var Y = date.getFullYear() + "-"
    var M =
        (date.getMonth() + 1 < 10
            ? "0" + (date.getMonth() + 1)
            : date.getMonth() + 1) + "-"
    var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " "
    var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ":"
    var m = (date.getMinutes() < 10
        ? "0" + (date.getMinutes())
        : date.getMinutes())
    var s = (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds())
    return M + D + h + m
}
// export const
 
function timeData (time) {
    var date
    if (time) {
        date = new Date(time)
    } else {
        date = new Date()
    }
    const Y = date.getFullYear()
    const M = date.getMonth() + 1
    const MM = (date.getMonth() + 1 < 10) ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
    const D = date.getDate()
    const DD = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate())
    const h = date.getHours()
    const hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours())
    const m = date.getMinutes()
    const mm = (date.getMinutes() < 10
        ? "0" + (date.getMinutes())
        : date.getMinutes())
    const s = date.getSeconds()
    const ss = (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds())
    return {
        Y, M, MM, D, DD, h, hh, m, mm, s, ss
    }
}
 
export const getEightHourTime = (time) => {
    const { Y, M, MM, DD, D, hh, h, mm, m, ss, s } = timeData(time)
    const now = `${Y}-${MM}-${DD} ${hh}:00:00`
    if (h > 8) {
        const before = `${Y}-${MM}-${DD} 08:00:00`
        return [before, now]
    } else {
        if (D - 1 > 0) {
            const yesD = (DD - 1 < 10) ? ('0' + DD - 1) : DD - 1
            const yesterday = `${Y}-${MM}-${yesD} 09:00:00`
            return [yesterday, now]
        } else {
            if (MM - 1 > 0) {
                const yesMM = (MM - 1 < 10) ? ('0' + MM - 1) : MM - 1
                const yesDay = new Date(Y, yesMM, 0).getDate()
                const lastMonDay = `${Y}-${yesMM}-${yesDay} 09:00:00`
                return [lastMonDay, now]
            } else {
                const lastY = Y - 1
                const lastYDay = `${lastY}-12-31 09:00:00`
                return [lastYDay, now]
            }
        }
    }
}
 
export const get24BeforeTime = (time) => {
    const { Y, M, MM, DD, D, hh, h, mm, m, ss, s } = timeData(time)
    const nowHour = `${Y}-${MM}-${DD} ${hh - 0 + 1}:00:00`
    const date = new Date(nowHour)
    const { Y: yY, MM: yM, DD: yD, hh: yH, mm: ymm, ss: yss } = timeData(date.setDate(date.getDate() - 1))
    const yesterDay = `${yY}-${yM}-${yD} ${yH}:00:00`
    return [yesterDay, nowHour]
}