js前端把获取的时间戳转化为正常的时间格式

js前端把获取的时间戳转化为正常的时间格式

 //时间戳转化为时间
      getTime(time) {
        var date=new Date(parseInt(time)* 1000);
        var year=date.getFullYear();
        var mon = date.getMonth()+1;
        var day = date.getDate();
        var hours = date.getHours();
        var minu = date.getMinutes();
        var sec = date.getSeconds();
        return year+'-'+this.getTimes(mon)+'-'+this.getTimes(day)+'T'+this.getTimes(hours)+':'+this.getTimes(minu)+':'+sec;
      },
      //将一位数字的时间补全
      getTimes(times){
        return  times<10?'0'+times:times;
      },

这就是将通过接口读取的时间戳转化为时间的方法

 return year+'-'+this.getTimes(mon)+'-'+this.getTimes(day)+'T'+this.getTimes(hours)+':'+this.getTimes(minu)+':'+sec;

在这个返回值可以自定义格式

发布了34 篇原创文章 · 获赞 5 · 访问量 2249

猜你喜欢

转载自blog.csdn.net/tanfei_/article/details/103948340