来自AI助手的总结
这段文章展示了如何在微信小程序中使用JavaScript和WXML获取并显示当前时间。
WXML
{{nowDate}}
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
© 版权声明
THE END










![根据 sitemap 的规则[0],当前页面 [pages/index/index] 将被索引-蚂蚁资源网](http://www.bkmayi.cn/wp-content/uploads/2024/12/e6a0b9e68dae-sitemap-e79a84e8a784e588990efbc8ce5bd93e5898de9a1b5e99da2-pages-index-index-e5b086e8a2abe7b4a2e5bc95_6773f72d52161.png)
暂无评论内容