美文网首页
js格式化时间戳

js格式化时间戳

作者: 路上灵魂的自由者 | 来源:发表于2019-03-14 15:47 被阅读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()))

    相关文章

      网友评论

          本文标题:js格式化时间戳

          本文链接:https://www.haomeiwen.com/subject/quqcmqtx.html