小程序在页面把时间戳转为时间日期格式,在js里面转为时间日期格式

时间:2024-02-22 08:57:57

1、在js中转换

先在utils文件夹下的util中写入下面代码

 1 function formatTimeTwo(number, format) {
 2   var formateArr = [\'Y\', \'M\', \'D\', \'h\', \'m\', \'s\'];
 3   var returnArr = [];
 4 
 5   var date = new Date(number);
 6   returnArr.push(date.getFullYear());
 7   returnArr.push(formatNumber(date.getMonth() + 1));
 8   returnArr.push(formatNumber(date.getDate()));
 9   returnArr.push(formatNumber(date.getHours()));
10   returnArr.push(formatNumber(date.getMinutes()));
11   returnArr.push(formatNumber(date.getSeconds()));
12 
13   for (var i in returnArr) {
14     format = format.replace(formateArr[i], returnArr[i]);
15   }
16   return format;
17 }
18 
19 module.exports = {
20   formatTimeTwo: formatTimeTwo
21 }

然后在要用的js页面引入

var time = require(\'../../utils/util.js\');
res.data.content.memberExpireTime = time.formatTimeTwo(res.data.content.memberExpireTime, \'Y-M-D\');
2、在页面引用
先在相应的文件夹下建一个fitter.wxs
 1 var filters = {
 2   toDate: function (value) {
 3     var time = getDate(value);
 4     var year = time.getFullYear();
 5     var month = time.getMonth() + 1;
 6     var date = time.getDate();
 7     var hour = time.getHours();
 8     var minute = time.getMinutes();
 9     var second = time.getSeconds();
10     month = month < 10 ? "0" + month : month;
11     date = date < 10 ? "0" + date : date;
12     hour = hour < 10 ? "0" + hour : hour;
13     minute = minute < 10 ? "0" + minute : minute;
14     second = second < 10 ? "0" + second : second;
15     return year + "-" + month + "-" + date
16   }
17 
18 }
19 
20 module.exports = {
21   toDate: filters.toDate
22 
23 }

然后在页面这样用

{{filters.toDate(content.forumTopicResponse.maxReturnTime)}}