linwe
2024-08-08 3c738f4fe2762bba8087e5a22fc0dc06560eab0e
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
export const calcDate = (date1, date2) => {
    let date3 = date2 - date1;
 
    let days = Math.floor(date3 / (24 * 3600 * 1000))
 
    let leave1 = date3 % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
    let hours = Math.floor(leave1 / (3600 * 1000))
 
    let leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
    let minutes = Math.floor(leave2 / (60 * 1000))
 
    let leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数
    let seconds = Math.round(date3 / 1000)
    return {
        leave1,
        leave2,
        leave3,
        days: days,
        hours: hours,
        minutes: minutes,
        seconds: seconds,
    }
}
 
/**
 * 日期格式化
 */
export function dateFormat(date, format) {
    format = format || 'yyyy-MM-dd hh:mm:ss';
    if (date !== 'Invalid Date') {
        let 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 (let 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, format) {
    if (timestamp && (typeof timestamp === "string") && timestamp.indexOf("-") > -1) {
        timestamp = timestamp.replace(/\-/g, "/")
    }
    let time = new Date(timestamp)
    let formatterTime = dateFormat(time, format)
    return formatterTime
}
 
 
//时间戳获取时间
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 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
}
 
//获取八点-当前时间或者昨天8点到今天7点时间
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 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 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()) + " ";
    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
}
 
export const getTimeBefore = (n = 8) => {
    const nowStamp = new Date().getTime()
    const beforeStamp = nowStamp - (n * 60 * 60 * 1000)
    const nowTime = timeStampTosecond(nowStamp + 1000 * 60 * 60)
    const beforeTime = timeStampTosecond(beforeStamp)
    return [beforeTime, nowTime]
}
 
//返回年月日时分秒
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";
}