js中设置时间格式

//time:vue elementui中时间控件的时间,’yyyy-MM-dd’格式
formatDate(time, ‘yyyy-MM-dd’);

function formatDate(time, format) {
    let t = new Date(time)
    let tf = function (i) {
        return (i < 10 ? '0' : '') + i
    }
    return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
        switch (a) {
            case 'yyyy':
                return tf(t.getFullYear())
            case 'MM':
                return tf(t.getMonth() + 1)
            case 'mm':
                return tf(t.getMinutes())
            case 'dd':
                return tf(t.getDate())
            case 'HH':
                return tf(t.getHours())
            case 'ss':
                return tf(t.getSeconds())
        }
    })
}
    原文作者:mhbsoft
    原文地址: https://blog.csdn.net/mhbsoft/article/details/86601077
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞