hyb
2026-01-30 15bc7727b58bf9ca0c8f21702fa893daac232b8d
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
/**
 * 将日期对象转化为指定格式的日期字符串
 * @param {Date} time - 日期对象
 * @param {string} format - 日期格式,支持YY-MM-DD hh:mm:ss
 * @return {string} - 格式化后的日期字符串
 */
export const datetimeObj2str = function(time, format = "YY-MM-DD hh:mm:ss") {
    let date = new Date(time);
    let year = date.getFullYear(),
        month = date.getMonth() + 1,
        day = date.getDate(),
        hour = date.getHours(),
        min = date.getMinutes(),
        sec = date.getSeconds();
    let preArr = Array.apply(null, Array(10)).map(function(elem, index) {
        return "0" + index;
    });
 
    return format
        .replace(/YY/g, preArr[year] || year)
        .replace(/MM/g, preArr[month] || month)
        .replace(/DD/g, preArr[day] || day)
        .replace(/hh/g, preArr[hour] || hour)
        .replace(/mm/g, preArr[min] || min)
        .replace(/ss/g, preArr[sec] || sec);
};
 
/**
 * 将时间戳转换为可读的时间格式
 * @param {Number} timestamp - 时间戳
 * @returns {String} - 可读的时间格式,例如:2022-04-12 13:30:45
 */
export const timestamp2time = function(timestamp) {
    if (!timestamp) {
        return "";
    }
    let date = new Date(timestamp * 1000);
    const Y = date.getFullYear() + "-";
 
    // js的月份从0开始
    const month = date.getMonth() + 1;
    const M = (month < 10 ? "0" + month : month) + "-";
 
    const days = date.getDate();
    const D = (days < 10 ? "0" + days : days) + " ";
 
    const hours = date.getHours();
    const h = (hours < 10 ? "0" + hours : hours) + ":";
 
    const minutes = date.getMinutes();
    const m = (minutes < 10 ? "0" + minutes : minutes) + ":";
 
    const seconds = date.getSeconds();
    const s = seconds < 10 ? "0" + seconds : seconds;
 
    return Y + M + D + h + m + s;
};