HTML & CSS & JavaScript - 一个简单数字时钟

一个简单数字时钟

在这里插入图片描述

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>一个简单的数字时钟</title>
        <style type="text/css">
            * {
                box-sizing: border-box;
                margin: 0;
                padding: 0;
            }
            body {
                background: linear-gradient(#000000, #841010);
                /* background-color: #000; */
                width: 100%;
                height: 100vh;
                display: flex;
                justify-content: center;
                align-items: center;
            }
            .container {
                /* border: 1px solid red; */
                width: 100%;
                height: 100%;
                display: flex;
                justify-content: center;
                align-items: center;
                user-select: none;
                font-family: "courier new";
                font-size: 120px;
                font-weight: 600;
                letter-spacing: 2px;
                color: rgba(255, 255, 255, 1);
            }
        </style>
    </head>
    <body>
        <div class="container"></div>
    </body>

    <script type="text/javascript">
        window.onload = (event) => {
            // console.log(event);
            main();
        }

        function main() {
            const p = document.querySelector(".container");
            
            showDatetime(p);
            
            window.setInterval(() => {
                showDatetime(p);
            }, 1000);
        }
        
        function showDatetime(target) {
            const date = new Date();
            console.log(date);
            console.log(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds());
            
            const year = date.getFullYear();
            const month = date.getMonth();
            const dayOfMonth = date.getDate();
            const hours = date.getHours();
            const minutes = date.getMinutes();
            const seconds = date.getSeconds();
            
            dateString = `${year}.${padZero(month, 2, true)}.${padZero(dayOfMonth, 2, true)}`;
            timeString = `${padZero(hours, 2, true)}:${padZero(minutes, 2, true)}:${padZero(seconds, 2, true)}`;
            
            // target.innerText = `${dateString} ${timeString}`;
            target.innerText = `${timeString}`;
            console.log(target.innerText);
        }
        
        function padZero(target, targetLength, atStart) {
            if (typeof target === "number") {
                if (target < 10) {
                    target = target.toString();
                } else {
                    return target;
                }
            }
            
            if (target.length === targetLength) {
                return target;
            }
            
            if (atStart === true) {
                return target.padStart(targetLength, "0");
            } else {
                return target.padEnd(targetLength, "0");
            }
        }
    </script>
</html>
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值