1:html
<view class="footerTitle">
<view>{{time}}</view>
</view>
2.js
data() {
return {
time: '',
timer: null
}
},
onLoad(option) {
let _this = this; // 声明一个变量指向Vue实例this,保证作用域一致
_this.time = _this.getTime()
this.timer = setInterval(() => {
_this.time = this.getTime(); // 修改数据date
}, 1000)
},
methods: {
getTime: function() { //获取当前时间
var date = new Date(),
month = date.getMonth() + 1,
day = date.getDate(),
hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
month >= 1 && month <= 9 ? (month = "0" + month) : "";
day >= 0 && day <= 9 ? (day = "0" + day) : "";
return month + '月' + day + '日' + ' ' + hour + ':' + minute + ':' + second;
}
}
效果