JS格式化中国标准时间

需求

获取时间的时候出现时间是中国标准时间,需要转化成想要的格式

代码

//定义格式化函数:
handleTime(time, format) {
    if (time == null || time == undefined || time == "") {
        return "";
    }
    var t = new Date(time);
    var 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());
                break;
            case 'MM':
                return tf(t.getMonth() + 1);
                break;
            case 'mm':
                return tf(t.getMinutes());
                break;
            case 'dd':
                return tf(t.getDate());
                break;
            case 'HH':
                return tf(t.getHours());
                break;
            case 'ss':
                return tf(t.getSeconds());
                break;
        }
    })
},
//调用函数
handleTime(me.data.formInline.kssj, "yyyy-MM-dd HH:mm:ss"),
原文地址:https://www.cnblogs.com/shuhao66666/p/15196342.html