mounted() {
this.updateTime(); // 初始化时间
setInterval(this.updateTime, 1000); // 每秒更新时间
},
updateTime() {
const date = new Date();
const hours = date.getHours().toString().padStart(2, '0');
const minutes = date.getMinutes().toString().padStart(2, '0');
const seconds = date.getSeconds().toString().padStart(2, '0');
this.currentTime = `${hours}:${minutes}:${seconds}`;
}
- <template>
- <view class="time-container">
- <view>{{ currentTime }}view>
- view>
- template>
- <script>
- export default {
- data() {
- return {
- currentTime: '' // 当前时间
- };
- },
- mounted() {
- this.updateTime(); // 初始化时间
- setInterval(this.updateTime, 1000); // 每秒更新时间
- },
- methods: {
- updateTime() {
- const date = new Date();
- const hours = date.getHours().toString().padStart(2, '0');
- const minutes = date.getMinutes().toString().padStart(2, '0');
- const seconds = date.getSeconds().toString().padStart(2, '0');
- this.currentTime = `${hours}:${minutes}:${seconds}`;
- }
- }
- };
- script>
- <style>
- .time-container {
- text-align: center;
- font-size: 24px;
- }
-
- style>