js动态获取当前时间并显示星期数

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .box {
            width: 300px;
            height: 150px;
            background-color: #2e3344;
            color: white;
            margin: 300px auto;
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
        }

        h2 {
            text-align: center;
            font-size: 60px;


        }

        p {
            text-align: center;
        }
        span{
            font-size: 20px;
            margin-right: 15px;
        }
    </style>
</head>

<body>
    <div class="box">
        <!-- 时分 -->
        <h2></h2>
        <p>
            <!-- 存储月日 -->
            <span class="one"></span>
            <!-- 存储星期 -->
            <span class="two"></span>
       
        </p>

    </div>



    <div></div>
    <script>
//获取对应的元素
        const div = document.querySelector('div')
        const h2 = document.querySelector('h2')
        const one = document.querySelector('.one')
        const two = document.querySelector('.two')
        const three = document.querySelector('.three')

//封装倒计时函数,根据需求返结果
        function getTime() {
            let date = new Date()//当前时间
            let year = date.getFullYear()//当前年份
            let month = date.getMonth() + 1//当前月
            let day = date.getDate()//天数
            let week = date.getDay()//星期数
            let hour = date.getHours()//时
            hour = hour < 10 ? '0' + hour : hour
            let minute = date.getMinutes()//分
            minute = minute < 10 ? '0' + minute : minute
            let second = date.getSeconds()//秒
            second = second < 10 ? '0' + second : second

            return ` ${hour}:${minute} `
        }
        h2.innerHTML = getTime()//获取当前时间的时,分
        //将星期数用数组包裹
        const arr = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
        setInterval(function () {
            h2.innerHTML = getTime()
        }, 1000)
        let date = new Date()
        one.innerHTML = `${date.getMonth() + 1}月${date.getDate()}日`//获取当前时间的月日
        two.innerHTML = `${arr[date.getDay()]}`//获取星期数

    </script>
</body>

</html>
相关推荐
ONLYOFFICE2 分钟前
ONLYOFFICE 全新 PDF 编辑器 API 上线,自动化处理 PDF 内容
前端·人工智能·pdf·编辑器·onlyoffice
James man2 分钟前
前端节点连接库选型指南:React-Flow、AntV X6 与 Power-Link 深度对比
前端·react.js·前端框架
于慨5 分钟前
java Web
java·开发语言·前端
徐小夕@趣谈前端5 分钟前
借助AI,1周,0后端成本,我们开源了一款Office预览SDK
前端·人工智能·开源·node.js·编辑器·github·格式工厂
Dxy12393102167 分钟前
HTML中图表学习:从基础到实战指南
前端·学习·html
We་ct7 分钟前
LeetCode 53. 最大子数组和:两种高效解法(动态规划+分治)
前端·算法·leetcode·typescript·动态规划·分治
我爱学习_zwj7 分钟前
设计模式-3(装饰器模式)
前端·设计模式·装饰器模式
Jinuss12 分钟前
React 19 新特性:`useOptimistic` Hook 完整指南
前端·javascript·react.js
清汤饺子13 分钟前
$20 的 Cursor Pro 额度,这样用一个月都花不完
前端·javascript·后端
a11177613 分钟前
MD 架构图生成器(html 开源)
前端·开源·html