微信小程序 /Date(1537326774000) 字符串(时间戳)转日期

微信小程序 /Date(1537326774000) 字符串(时间戳)转日期

 

首先,把util.js里的内容替换成下列代码。

 

年月日时分
// util.js
//时间戳转换成日期时间


function date_time(val) {


  var date = new Date(parseInt(val.replace("/Date(", "").replace(")/", ""), 10));


  //月份为0-11,所以+1,月份小于10时补个0
  var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;


  var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();


  var hour = date.getHours();


  var minute = date.getMinutes();


  var second = date.getSeconds();


  var theTime = date.getFullYear() + "-" + month + "-" + currentDate + " " + hour + ":" + minute + ":" + second;

  return theTime ;


}
module.exports = {


  date_time: date_time


}

其次,在需要转换时间戳的地方 引用 util.js

var util = require("../../utils/util.js");

 

wx.request({

    url: 'url',

header: {

    'content-type': 'application/json'

},

success:function(res) {

    var datas = res.data.data;

    for (var i in datas) {

        datas[i].CreateTime = util.date_time(datas[i].CreateTime)

         //这里是主要代码,获取数据中的【CreateTime: /Date(1537326774000)/】

    }

    that.setData({

        news: datas,

    })

    console.log(res.data.data)//通过打印可以看到 已经将/Date(1537326774000)/ 转换成2018-09-19 11:12:54

 

    }

})

  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值