js时间格式化

const format = (time, format = 'yyyy-mm-dd') => {
    const d = new Date(time);
    const t = (i) => { return (i < 10 ? '0' : '') + i };

    const year = d.getFullYear();
    const month = d.getMonth() + 1;
    const day = d.getDate();
    const hour = d.getHours();
    const minutes = d.getMinutes();
    const seconds = d.getSeconds();
    const weekday = d.getDay();

    return format.replace(/(yy){1,2}|m{1,2}|d{1,2}|h{1,2}|i{1,2}|s{1,2}|w{1,2}/gi, function(r) {
        switch (r.toUpperCase()) {
            case 'YY':
                return ('' + year).substr(2);
            case 'YYYY':
                return year;
            case 'M':
                return month;
            case 'MM':
                return t(month);
            case 'D':
                return day;
            case 'DD':
                return t(day);
            case 'H':
                return hour;
            case 'HH':
                return t(hour);
            case 'I':
                return minutes;
            case 'II':
                return t(minutes);
            case 'S':
                return seconds;
            case 'SS':
                return t(seconds);
            case 'W':
                return `星期${['日', '一', '二', '三', '四', '五', '六'][weekday]}`;
            case 'WW':
                return ['Sunday', 'Monday', 'TuesDay', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][weekday];
        }
    });
}

export default format
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值