js高手時間毫秒轉格式

//格式化時間orm

var formatDate = function(time, format){get

    var t = new Date(time);it

    var tf = function(i){return (i < 10 ? '0' : '') + i};io

    return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){console

        switch(a){function

            case 'yyyy':form

                return tf(t.getFullYear());im

                break;時間

            case 'MM':co

                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;

        }

    })

}

console.log(formatDate(new Date().getTime(), 'yyyy-MM-dd HH:mm:ss'))

相關文章
相關標籤/搜索