微信小程序json时间格式化处理

//打开微信小程序后,部分模板绑定数据是通过接口调取的,当遇到数据的时间被json格式化后,需要正常的显示。可以通过扩展一个方法去处理时间。
1.打开utils里的utils.js  ,也可以按照自己的习惯添加我们需要扩展的函数renderTime,方法如下
function renderTime(date) {
       var da = new Date(parseInt(date.replace("/Date(", "").replace(")/", "").split("+")[0]));
       var  Year = da.getFullYear(); //ie火狐下都可以
          var  Month = da.getMonth() + 1;
          var  Day = da.getDate();
          var  Hours=da.getHours();
          var  Minutes=da.getMinutes();
          var  Seconds=da.getSeconds();
            var CurrentDate = "";
            CurrentDate += Year + "-";
            if (Month >= 10) {
                CurrentDate += Month + "-";
            }
            else {
                CurrentDate += "0" + Month + "-";
            }
            if (Day >= 10) {
                CurrentDate += Day;
            }
            else {
                CurrentDate += "0" + Day;
            }
            if (Hours <10) {
                 Hours = "0" + Hours;
            }
            if (Minutes < 10) {
                Minutes ="0"+ Minutes;
            }
            if (Seconds < 10) {
                  Seconds ="0"+ Seconds;
            }
            return CurrentDate + " " + Hours + ":" + Minutes + ":" + Seconds;
  }
//引用的话,只需要在你的页面下的js里
var util = require('../../utils/util.js');   你放置这个函数。
当然,utils里的
//扩展的方法需要在Module里去声明
module.exports = {
  renderTime:renderTime
}
调用方法。便是  util.renderTime(date);即可。

 

posted @ 2017-03-09 10:15  大楚打码人  阅读(9488)  评论(0编辑  收藏  举报