WXML
<view class="classname">{{nowDate}}</view>
JS
var util = require('../../utils/util.js') Page({ data: { nowDate:'' }, onLoad: function () { this.setData({ nowDate: util.formatTime(new Date()) }); } })
util.js
function formatTime(date) { 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 + "日" + hour + "时" + minute + "分" + second+"秒"; } //下面的代码很重要,如果没有,则外部调用formatTime函数的时候不会生效 module.exports = { formatTime: formatTime }
解释:
首先包含公共js util.js
首次加载的时候调用util.formatTime(new Date())获取当前时间,并赋值于nowDate
通过this.setData把值渲染到wxml