小程序获取当前日期和时间
本案例是从页面初始化简化出来的。如有详细代码需要,欢迎加群,找到今日日期代码下载。
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+"秒";
}
module.exports = {
formatTime: formatTime
}