js的时间戳的转换

时间:2021-02-28 02:21:52
let formatDate = function(value, type) {
    let date = new Date(value * 1000),
        Y = date.getFullYear(),
        m = date.getMonth() + 1,
        d = date.getDate(),
        h = date.getHours(),
        i = date.getMinutes(),
        s = date.getSeconds();
    let mm = m,
        dd = d,
        hh = h,
        ii = i,
        ss = s;

    let t = '';

    if (m < 10) {
        mm = '0' + m;
    }
    if (d < 10) {
        dd = '0' + d;
    }
    if (hh < 10) {
        hh = '0' + hh;
    }
    if (i < 10) {
        ii = '0' + i
    }
    if (s < 10) {
        ss = '0' + s;
    }

    switch (type) {
        case 0 || undefined:
            t = Y + '-' + mm + '-' + dd;
            break;
        case 1:
            t = Y + '-' + mm + '-' + dd;
            break;
        case 2:
            t = Y + '/' + m + '/' + d + ' ' + hh + ':' + ii;
            break;
        case 3:
            t = Y + '/' + m + '/' + d;
            break;
        case 4:
            t = Y + '年' + m + '月' + d + '日' + ' ' + hh + ':' + ii;
            break;
        case 5:
            t = Y + '-' + mm + '-' + dd + ' ' + hh + ':' + ii + ':' + ss;
            break;
        case 6:
            t = Y + '年' + m + '月' + d + '日' + ' ' + hh + ':' + ii;
            break;
        case 7:
            t = Y + '年' + m + '月' + d + '日';
            break;
        case 8:
            t = m + '.' + d;
            break;
    }

    return t;
};