【Avalon源碼】dateGetter

function dateGetter(name, size, offset, trim) {
    return function(date) {
        var value = date["get" + name]()
        if (offset > 0 || value > -offset)
            value += offset
        if (value === 0 && offset === -12) { // hh h的狀況
            value = 12
        }
        return padNumber(value, size, trim)
    }
}

var DATE_FORMATS = {
    yyyy: dateGetter("FullYear", 4),
    yy: dateGetter("FullYear", 2, 0, true),
    y: dateGetter("FullYear", 1),
    MM: dateGetter("Month", 2, 1),
    M: dateGetter("Month", 1, 1),
    dd: dateGetter("Date", 2),
    d: dateGetter("Date", 1),
    HH: dateGetter("Hours", 2),
    H: dateGetter("Hours", 1),
    hh: dateGetter("Hours", 2, -12),
    h: dateGetter("Hours", 1, -12),
    mm: dateGetter("Minutes", 2),
    m: dateGetter("Minutes", 1),
    ss: dateGetter("Seconds", 2),
    s: dateGetter("Seconds", 1),
    sss: dateGetter("Milliseconds", 3)
}

console.log(DATE_FORMATS.HH(new Date()))
相關文章
相關標籤/搜索