js格式化时间戳

/*

    * 格式化时间戳

    *

*/

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()))

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容