Utils文件夹:存放工具类
utils.js(新建微信小程序系统自动生成的内容解析)
// 格式化时间的方法(将date数据格式转成“年/月/日 时:分:秒”格式)
const formatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
// JavaScript Array map() 方法(map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。)
// map() 方法按照原始数组元素顺序依次处理元素。
// 注意: map() 不会对空数组进行检测。
// 注意: map() 不会改变原始数组。
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
// 数字转字符串且小于10补0
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
module.exports = {
formatTime: formatTime
}
utils.js在logs.js(新建微信小程序系统自动生成的)中调用:显示用户登录时间
//添加引用
const util = require('../../utils/util.js')
//登陆函数调用
onLoad: function () {
this.setData({
logs: (wx.getStorageSync('logs') || []).map(log => {
return util.formatTime(new Date(log))
})
})
}
最后结果:logs页面显示内容logs.PNG