wxml 里怎样触发函数把时间戳转成时间 研究好几天解决不了
发布于 5 年前 作者 tao80 11293 次浏览 来自 问答
  • 当前 Bug 的表现(可附上截图)

new Date 报错

Date(时间戳)  报错

我需要在wxml 里面触发函数把时间戳转换时间 。一直解决不了。哪位老师帮忙指点一下。

4 回复

它和js的写法有一点点差别:

var timestampToDate = function(format, timestamp) {
      if(!timestamp) {
        return timestamp
      }
 
        var date = getDate(parseInt(timestamp))
        var year = date.getFullYear(),
            month = date.getMonth() + 1,
            day = date.getDate(),
            hour = date.getHours(),
            minute = date.getMinutes(),
            second = date.getSeconds();
      var re = getRegExp("[YyMmDdHhSs]+", 'g');
        var str = format.replace(re, function(w) {
 
            if(w == 'yy' || w == 'YY' || w == 'y' || w == 'Y') {
                return year.toString().substring(2);
 
            } else if(w == 'yyyy' || w == 'YYYY') {
                return year;
 
            } else if(w == 'MM') {
                return month >= 10 ? month : '0' + month;
 
            } else if(w == 'M') {
                return month;
 
            } else if(w == 'DD' || w == 'dd') {
                return day >= 10 ? day : '0' + day;
 
            } else if(w == 'D' || w == 'd') {
                return day;
 
            } else if(w == 'HH' || w == 'hh') {
                return hour >= 10 ? hour : '0' + hour;
 
            } else if(w == 'H' || w == 'h') {
                return hour;
 
            } else if(w == 'mm') {
                return minute >= 10 ? minute : '0' + minute;
 
            } else if(w == 'm') {
                return minute;
 
            } else if(w == 'ss' || w == 's') {
                return second >= 10 ? second : '0' + second;
            }
        });
         
        return str;
}
 
module.exports = {
    timestampToDate: timestampToDate
}

我需要在wxml 里面遍历数据的时候  每遇到时间戳  就触发函数转换时间。

定义个变量不行?

回到顶部