当前位置:网站首页>页面动态显示时间(升级版)
页面动态显示时间(升级版)
2022-04-23 06:24:00 【资深咸鱼~】
实现按钮控制时间展示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>页面动态显示时间</title>
</head>
<body>
<div>
<p class="p1"></p>
<button class="btn">停止时间</button>
</div>
<script>
//格式:2020年04月12日 10:20:00 星期二
var p1 = document.querySelector(".p1")
var btn = document.querySelector(".btn")
var timerId = setInterval(format, 1000)
var flag = true
function format() {
var newDate = new Date()
var day = newDate.getDay()
var y = newDate.getFullYear()
var m = (newDate.getMonth() + 1) < 10 ? "0" + (newDate.getMonth() + 1) : (newDate.getMonth() + 1)
var d = newDate.getDate() < 10 ? "0" + newDate.getDate() : newDate.getDate()
var h = newDate.getHours() < 10 ? "0" + newDate.getHours() : newDate.getHours()
var min = newDate.getMinutes() < 10 ? "0" + newDate.getMinutes() : newDate.getMinutes()
var s = newDate.getSeconds() < 10 ? "0" + newDate.getSeconds() : newDate.getSeconds()
var dict = {
1: "一",
2: "二",
3: "三",
4: "四",
5: "五",
6: "六",
0: "天",
}
//var week=["日","一","二","三","四","五","六"]
var matDate = y + "年" + m + "月" + d + "日" + h + ":" + min + ":" + s + " 星期" + dict[day]
p1.textContent = matDate
}
btn.onclick = function () {
if (flag) {
//停止时间
clearInterval(timerId)
btn.textContent = "开始时间"
timerId = null
flag = false
} else {
btn.textContent = "停止时间"
timerId = setInterval(format, 1000)
flag = true
}
}
</script>
</body>
</html>
效果展示:
版权声明
本文为[资深咸鱼~]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_51644702/article/details/124119552
边栏推荐
猜你喜欢
随机推荐
UDP基础学习
免费开源智能充电桩物联网SAAS云平台
# 可视化常见绘图(二)折线图
Moment.js中format方法函数的格式
Typora操作技巧说明(一)
remote: Support for password authentication was removed on August 13, 2021.
Pycharm
[LNOI2014]LCA——树链剖分——多点LCA深度和问题
通用型冒泡、选择、插入、希尔、快速排序的代码实现
可视化常见问题解决方案(八)数学公式
小程序换行符\n失效问题解决-日常踩坑
7.子查询
Emergency air space integrated communication system scheme of Guangxi Power Grid
技术小白的第一篇(表达自己)
H5案例开发
Machine vision series (02) -- tensorflow2 3 + win10 + GPU installation
How to improve the service efficiency of the hotel without blind spots and long endurance | public and Private Integrated walkie talkie?
Typora语法详解(一)
海康威视面经总结
记录一个查询兼容性的网站,String.replaceAll()兼容性报错