微信小程序列表时间戳转换

第一步先写js   随便命名为times.js

function toDate(number){

    var n=number * 1000;
    var date = new Date(n);
    var Y = date.getFullYear() + '/';
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '/';
    var D = date.getDate()<10 ? '0'+date.getDate() : date.getDate();
    return (Y+M+D)
}
 
module.exports = {
    toDate: toDate
  }
第二步,在需要转换的js文件中
2.1引入
var times = require('../../utils/times.js');
 
 2.2测试数据

 

 2.3逻辑代码,因为是测试数据,没有请求后台数据
  onLoad: function (options) {
    var that = this       //很重要,一定要写
    console.log(that.data.lists)
    for(var i=0;i<that.data.lists.length;i++){
      that.data.lists[i]["times"] = times.toDate(that.data.lists[i]["times"])
    }
    that.setData({
      lists:that.data.lists
    })
  },

 2.4页面层渲染

 2.5最后,大功告成,效果如下:

猜你喜欢

转载自www.cnblogs.com/pzkperson/p/11655698.html
今日推荐