Js实现网页时钟(备用)
发表于:2022-04-18 | 分类: 前端
字数统计: 266 | 阅读时长: 1分钟 | 阅读量:
js定时器实现一个网页时钟,直接贴代码(Vue框架环境)
1
2
3
4
5
6
7
8
<template>
<div>
<div class="he-sjzt">
<div class="he-xs">{{ dateTime }}</div>
<div class="he-nyr">{{ yearTime }}</div>
</div>
</div>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<script>
export default {
data() {
return {
dateTime: "",
yearTime: "",
};
},
mounted() {
this.time();
},
methods: {
time() {
// 获取当前时间
window.setInterval(() => {
const yy = new Date().getFullYear();
const mm =
new Date().getMonth() + 1 < 10
? "0" + (new Date().getMonth() + 1)
: new Date().getMonth() + 1;
const dd =
new Date().getDate() < 10
? "0" + new Date().getDate()
: new Date().getDate();
const hh = new Date().getHours();
const mf =
new Date().getMinutes() < 10
? "0" + new Date().getMinutes()
: new Date().getMinutes();
const ss =
new Date().getSeconds() < 10
? "0" + new Date().getSeconds()
: new Date().getSeconds();
this.yearTime = yy + "-" + mm + "-" + dd;
this.dateTime = hh + ":" + mf + ":" + ss;
}, 1000);
},
}
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<style scoped>
.he-sjzt {
position: absolute;
top: 17px;
display: inline-block;
width: 100px;
}
.he-xs {
font-size: 22px;
font-weight: 400;
text-align: center;
}
.he-nyr {
font-size: 16px;
font-weight: 400;
text-align: center;
}
</style>
上一篇:
Gis二次开发相关文件处理
下一篇:
Vue中clearInterval失效问题