Blame view

utils/time-process.js 1.13 KB
biao authored
1 2 3 4 5 6
/**
 * 需要格式化的时间格式
 */

'use strict';
姜枫 authored
7
const helpers = global.yoho.helpers;
biao authored
8 9

const timeFormat = {
10
    y: '剩{y}年{M}月{d}天',
11
    M: '剩{M}月{d}天',
biao authored
12 13 14 15 16 17 18 19 20 21 22 23
    d: '剩{d}天',
    h: '剩{h}小时',
    m: '剩{m}分钟',
    s: '剩{s}秒',
    dh: '剩{d}天{h}小时',
    dhms: '剩{d}天{h}小时{m}分钟{s}秒',
    hms: '剩{h}小时{m}分钟{s}秒',
    ms: '剩{m}分钟{s}秒'
};

const anHour = 3600;
const aDay = anHour * 24;
24 25
const aMonth = aDay * 30;
const aYear = aMonth * 12;
biao authored
26 27

/**
ccbikai authored
28
 * 折扣专场专题列表过期时间处理 单位:s
biao authored
29 30 31 32 33 34 35 36 37 38 39
 * @param  {[string]} time
 * @return {[object]}
 */
const processTime = (time) => {
    let data = {};
    let type = '';

    if (time < anHour) {
        data.warnColor = true;
        data.time = '低于1小时';
    } else {
40 41
        if (time > aYear) {
            type = 'y';
42 43
        } else if (time > aMonth) {
            type = 'M';
44
        } else if (time > aDay) {
biao authored
45 46 47 48 49 50 51 52 53 54 55 56
            type = 'dh';
        } else {
            type = 'h';
        }

        data.time = helpers.dateDiffFormat(timeFormat[type], time, 's');
    }

    return data;
};

module.exports = processTime;