js时间戳转化时间格式

// 判断是否前面补0
    add0 (m) {
      return m < 10 ? '0' + m : m
    },
    // 时间转化
    timeFormat (timestamp) {
    // timestamp是整数,否则要parseInt转换,不会出现少个0的情况
      var time = new Date(timestamp)
      // var year = time.getFullYear()
      // var month = time.getMonth() + 1
      // var date = time.getDate()
      var hours = time.getHours()
      var minutes = time.getMinutes()
      var seconds = time.getSeconds()
      // return year + '-' + this.add0(month) + '-' + this.add0(date) + ' ' + this.add0(hours) + ':' + this.add0(minutes) + ':' + this.add0(seconds)
      return this.add0(hours) + ':' + this.add0(minutes) + ':' + this.add0(seconds)
    },

// 时间戳转化为时间
            formatDateTime:function (timeStamp) { 
                if(timeStamp != null&&timeStamp != 0){
                    var date = new Date();
                    date.setTime(timeStamp * 1000);
                    var y = date.getFullYear();    
                    var m = date.getMonth() + 1;    
                    m = m < 10 ? ('0' + m) : m;    
                    var d = date.getDate();    
                    d = d < 10 ? ('0' + d) : d;    
                    var h = date.getHours();  
                    h = h < 10 ? ('0' + h) : h;  
                    var minute = date.getMinutes();  
                    var second = date.getSeconds();  
                    minute = minute < 10 ? ('0' + minute) : minute;    
                    second = second < 10 ? ('0' + second) : second;   
                    // return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;  
                    return y + '-' + m + '-' + d;  
                }else{
                    return ''
                }
            }

 

 

 这只是为了方便,纯属记录!

// 时间戳转化为时间
formatDateTime:function (timeStamp) {
if(timeStamp != null&&timeStamp != 0){
var date = new Date();
date.setTime(timeStamp * 1000);
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
var h = date.getHours();
h = h < 10 ? ('0' + h) : h;
var minute = date.getMinutes();
var second = date.getSeconds();
minute = minute < 10 ? ('0' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
// return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
return y + '-' + m + '-' + d;
}else{
return ''
}
}
posted @ 2018-04-22 11:39  丨黑寡妇丨  阅读(182)  评论(0编辑  收藏  举报