微信小程序动态获取当前时间
1,动态获取当前时间
2,在根目录下utils文件夹创建utils.js写获取时间方法
utils.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].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
function formatNumber(n) {
n = n.toString()
return n[1] ? n : '0' + n
}
module.exports = {
formatTime
}
3,页面动态获取当前时间
wxml
<view class="currentTime">
<view>当前时间:{
{time}}</view>
</view>
js
var util = require('../../../utils/utils.js');
Page({
/**
* 页面的初始数据
*/
data: {
time: '',
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
let that = this;
setInterval(function () {
that.setData({
time: util.formatTime(new Date())
});
}, 1000);
},
//点击获取当前点击时间
getTime: function () {
let that = this;
let currentTime = util.formatTime(new Date());
that.setData({
time: currentTime
})
},
})
还没有评论,来说两句吧...