站点图标 起风网

小程序在wxs文件或js文件时间格式化

小程序模块时间格式化

common.wxs   方法

filters={
formatTime:function (unixtime) {
var date = getDate(parseInt(unixtime) * 1000)
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds()
return [year, month, day].map(filters.formatNumber).join(‘/’) + ‘ ‘ + [hour, minute, second].map(filters.formatNumber).join(‘:’)
},
formatNumber:function (n) {
n = n.toString()
return n[1] ? n : ‘0’ + n
}
}
module.exports = {
formatTime: filters.formatTime,
}
wxml页面使用
头部引入
<wxs module=”common” src=”../../utils/common.wxs”></wxs>
使用
<view class=”right”>{{common.formatTime(orderDetail.create_time)}}</view>
common.js    js方法
function formatTime(unixtime) {
var date = new Date(parseInt(unixtime) * 1000)
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds()
return [year, month, day].map(formatNumber).join(‘/’) + ‘ ‘ + [hour, minute, second].map(formatNumber).join(‘:’)
}
function formatNumber(n) {
n = n.toString()
return n[1] ? n : ‘0’ + n
}
module.exports = {
formatTime: formatTime
}
在page/index.js 格式化
退出移动版