用js将从后台得到的13位时间戳(毫秒数)转换为想要的日期格式

function formatterTime(time, fmt) {

      if(!time){

            return '';

      }

      if(typeof(time) == "object" || typeof(time) == "OBJECT") {

      var z = {

                  M: time.getMonth() + 1,

                  d: time.getDate(),

                  h: time.getHours(),

                  m: time.getMinutes(),

                  s: time.getSeconds()

            };

      fmt = fmt.replace(/(M+|d+|h+|m+|s+)/g, function(v) {

                  return ((v.length > 1 ? "0" : "") + eval('z.' + v.slice(-1))).slice(-2);

            });

      return fmt.replace(/(y+)/g, function(v) {

                  return time.getFullYear().toString().slice(-v.length);

            });

      }else if(typeof(time) == "number"){

            var TIME = new Date( time) ;

            var z = {

                        M: TIME.getMonth() + 1,

                        d: TIME.getDate(),

                        h: TIME.getHours(),

                        m: TIME.getMinutes(),

                        s: TIME.getSeconds()

                  };

            fmt = fmt.replace(/(M+|d+|h+|m+|s+)/g, function(v) {

                        return ((v.length > 1 ? "0" : "") + eval('z.' + v.slice(-1))).slice(-2);

                  });

            return fmt.replace(/(y+)/g, function(v) {

                        return TIME.getFullYear().toString().slice(-v.length);

                  });

      }

      else return time;

}
formatterTime(1565780223000 ,"yyyy-MM-dd hh:mm:ss")
posted @ 2019-08-21 15:27  捞档哥  阅读(2739)  评论(0编辑  收藏  举报