通過vue在uni-app中設置當前北京時間(動態時間)


在項目中通常需要在頁面中顯示當前時間,具體代碼如下所示:

<template>
	<div class="container">
		<div class="times">
			<span>{{ hour }} : {{ minutes }}</span>
			<span style="font-size: 38upx;margin-left: 10upx;;">{{ seconds }}s</span>
		</div>
	</div>
</template>

<script>
	export default {
		data() {
			const now = new Date()
			return {
				hour: now.getHours() < 10 ? '0' + now.getHours() : now.getHours(), //當小時為個為數時在在前加0(例:01),以下同理
				minutes: now.getMinutes() < 10 ? '0' + now.getMinutes() : now.getMinutes(),
				seconds: now.getSeconds() < 10 ? '0' + now.getSeconds() : now.getSeconds(),
				timer: null
			}
		},
		onLoad() {
			if (this.timer) {
				clearInterval(this.timer)
			}
			setInterval(() => {
				this.getTime()
			},1000)  //設置定時器,時時間每隔一秒鍾走一次(即每秒)
		},
		methods: {
			getTime () {
				var date = new Date()
				this.hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
				this.minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
				this.seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
			}
		}

	}
</script>
<style scoped>
	.container {
		margin: 0 auto;
	}

	.times {
		margin-left: 250upx ;
		font-size: 60upx;
		padding-top: 60upx;
		font-weight: bold;
	}
</style>

-----
通過以上的設置就可以實現設置當前北京時間(動態時間)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM