(共566篇)
全部分类

js格式化获取到的日期对象xxxx-xx-xxxx:xx
[ JS基础 ] 

参数 类型 是否可以为空
baseDate object:Date/Timestamp:string 不能为空
baseFormat string 可为空
 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
function formatDate(baseDate, baseFormat) {
    let date;
    if (!baseDate) {
        throw new Error('first parameter must be pass.');
    }
    if (!baseFormat) {
        baseFormat = 'yy-mm-dd hh:ii:ss';
    }
    if (Object.prototype.toString.call(baseDate) === '[object Date]') {
        date = baseDate;
    } else if (/^\d{10,13}$/.test(String(baseDate))) {
        date = new Date(baseDate);
    } else {
        throw new Error('first parameter must be [Object date] or timestamp.');
    }

    const padZero = function (str) {
        return str < 10 ? '0' + str : str;
    };
    const year = date.getFullYear();
    const month = padZero(date.getMonth() + 1);
    const day = padZero(date.getDate());
    const hour = padZero(date.getHours());
    const minute = padZero(date.getMinutes());
    const seconds = padZero(date.getSeconds());
    let str = baseFormat.replace('yy', year).replace('mm', month).replace('dd', day).replace('hh', hour).replace('ii', minute).replace('ss', seconds);
    console.log(str);
}

formatDate(new Date());