首页 > 微信 > 详细

小程序对mongodb日期显示处理问题

时间:2020-03-19 00:45:25      阅读:87      评论:0      收藏:0      [点我收藏+]

描述:微信小程序开发后端数据库使用的是mongodb。其中有个字段为日期类型,这样在查询数据显示的时候如下:/Date(1584538219495)/   为了能够正常的显示日期需要使用js处理一下。

 

js函数

  //处理日期
  changeDateFormat: function (cellval){
    var dateVal = cellval + "";
    if (cellval != null) {
      var date = new Date(parseInt(dateVal.replace("/Date(", "").replace(")/", ""), 10));
      var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
      var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();

      var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
      var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
      var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

      return date.getFullYear() + "-" + month + "-" + currentDate + " " + hours + ":" + minutes + ":" + seconds;
    }
  },

调用:

    // var dd = "/Date(1584538219495)/"
    // var mm = this.changeDateFormat(dd);
    // console.log(mm);

技术分享图片

 

 在这里遍历数组,对里面的日期字段进行处理。处理之后就可以正常显示了。

 

小程序对mongodb日期显示处理问题

原文:https://www.cnblogs.com/xubao/p/12520911.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!