<template>
	<view>
		<view>{{date}}</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
				date:'',
				setTime:''
			}
		},
		onLoad() {
			var setTime = setInterval( ()=> {
				let date = new Date()
				let year = date.getFullYear()
				let month = date.getMonth() + 1
				month >= 9 ? month :  month='0'+month
				let day = date.getDate()
				day >= 9 ? day : day = '0'+ day
				let hour = date.getHours()
				hour >= 9 ? hour : hour = '0'+hour 
				let minute = date.getMinutes()
				minute >= 9 ?  minute : minute = '0'+minute
				let second = date.getSeconds()
				second >= 9 ? second : second = '0'+second
				this.date = year + '-'+month+'-'+day+"  "+hour+":"+minute+":"+second
			}, 1000)
		},
		onHide(){
			clearInterval(setTime)
		},
		methods: {

		}
	}
</script>

时间可以实时动态变化:

使用定时器,每次过一秒就重新调用Date()来获取时间,然后在界面销毁时,清除定时器。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐