js格式化时间戳

2019-03-14  本文已影响0人  路上灵魂的自由者

/*

    * 格式化时间戳

    *

*/

let date = {

    judge: function(m){

        return m<10?'0'+m:m;

    },

    timeStampformatYMD: function(shijianchuo){

        /*

            * shijianchuo 时间戳ß

            * 转换年月日

        */

        if(!shijianchuo){

            return '--'

        }

        var shijianchuo = parseInt(shijianchuo)

        var time = new Date(shijianchuo);

        var yy = time.getFullYear();

        var m = time.getMonth()+1;

        var dd = time.getDate();

        var hh = time.getHours();

        var mm = time.getMinutes();

        var ss = time.getSeconds();

        return yy+'-'+ date.judge(m)+'-'+date.judge(dd);

    },

    timeStampformatYMDHMS: function(shijianchuo){    

        /*

            * shijianchuo 时间戳ß

            * 转换年月日时分秒

        */

        if(!shijianchuo){

            return '--'    

        }

        var shijianchuo = parseInt(shijianchuo)

        var time = new Date(shijianchuo);

        var yy = time.getFullYear();

        var m = time.getMonth()+1;

        var dd = time.getDate();

        var hh = time.getHours();

        var mm = time.getMinutes();

        var ss = time.getSeconds();

        return yy+'-'+ date.judge(m)+'-'+date.judge(dd)+' '+date.judge(hh)+':'+date.judge(mm)+':'+date.judge(ss);

    },

    newDateformatYMD: function(newDate){

        if(!newDate){

            return '--'

        }

        var time = new Date(newDate.getTime());

        var yy = time.getFullYear();

        var m = time.getMonth()+1;

        var dd = time.getDate();

        var hh = time.getHours();

        var mm = time.getMinutes();

        var ss = time.getSeconds();

        return yy+'-'+ date.judge(m)+'-'+date.judge(dd)

    },

    newDateformatYMDHMS: function(newDate){

        if(!newDate){

            return '--'

        }

        var time = newDate;

        var yy = time.getFullYear();

        var m  = time.getMonth()+1;

        var dd = time.getDate();

        var hh = time.getHours();

        var mm = time.getMinutes();

        var ss = time.getSeconds();

        return yy+'-'+ date.judge(m)+'-'+date.judge(dd)+' '+date.judge(hh)+':'+date.judge(mm)+':'+date.judge(ss);

    }

}

console.log(date.timeStampformatYMD(1552439828000))

console.log(date.timeStampformatYMDHMS(1552439828000))

console.log(date.newDateformatYMD(new Date()))

console.log(date.newDateformatYMDHMS(new Date()))

上一篇下一篇

猜你喜欢

热点阅读