当前时间(模拟数字时钟)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>模拟电子时钟</title>
    <script src="https://cdn.bootcss.com/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
    <div>
        当前时间为: 
        <span class="current-time"></span>
    </div>

    <script>
        $(function () {
            // 获取时间
            function currentTime() {
                let time, y, m, d, h, mm, s;
                let date = new Date();
                y = date.getFullYear();
                m = date.getMonth() + 1;
                d = date.getDate();

                h = date.getHours();
                mm = date.getMinutes();
                s = date.getSeconds();

                $(".current-time").html(filterTime(y, m, d, h, mm, s));

                setInterval(function () {
                    let date2 = new Date();
                    h = date2.getHours();
                    mm = date2.getMinutes();
                    s = date2.getSeconds();

                    $(".current-time").html(filterTime(y, m, d, h, mm, s));
                }, 1000)
            }

            // 过滤时间
            function filterTime(y, m, d, h, mm, s) {
                time = y + "年";
                if (m < 10) {
                    time += "0";
                }
                time += m + "月";
                if (d < 10) {
                    time += "0";
                }
                time += d + "日";
                if (h < 10) {
                    time += "0";
                }
                time += h + "时";
                if (mm <10 ) {
                    time += "0";
                }
                time += mm + "分";
                if (s < 10) {
                    time += "0";
                }
                time += s + "秒";

                return time;
            }

            currentTime();
        })
    </script>
</body>
</html>

 

### 回答1: 这是一个使用 Python 创建模拟数字时钟的示例代码: ```python import time import os while True: # 获取当前时间 current_time = time.strftime("%I:%M:%S %p") # 清空屏幕 os.system("cls") # 打印当前时间 print(current_time) # 每秒更新一次 time.sleep(1) ``` 这段代码将循环不断地获取当前时间,并在每秒更新一次。它使用了 Python 中的 `time` 和 `os` 模块,其中 `time` 模块用于获取当前时间,`os` 模块用于清空屏幕。 你可以按照你喜欢的格式打印时间,比如只打印小时和分钟,或者使用不同的字符来分隔时间的各个部分。可以使用 `time.strftime` 函数来格式化时间。 希望这个示例代码能帮到你! ### 回答2: 下面是一个使用Python编写的模拟数字时钟的代码示例: ```python import time def display_time(): current_time = time.strftime('%H:%M:%S') # 获取当前时间 digits = { '0': [[' ### '], [' # # '], ['# #'], ['# #'], ['# #'], [' # # '], [' ### ']], '1': [[' # '], [' ## '], [' # # '], [' # '], [' # '], [' # '], [' ##### ']], '2': [[' ### '], [' # # '], ['# #'], [' # '], [' # '], [' # '], [' ##### ']], '3': [[' ### '], [' # # '], [' # '], [' ## '], [' # '], [' # # '], [' ### ']], '4': [[' # '], [' ## '], [' # # '], [' # # '], [' ##### '], [' # '], [' # ']], '5': [[' ##### '], [' # '], [' #### '], [' # '], [' # '], [' # # '], [' ### ']], '6': [[' ### '], [' # # '], ['# '], ['#### '], ['# # '], [' # # '], [' ### ']], '7': [[' ##### '], [' # '], [' # '], [' # '], [' # '], [' # '], [' # ']], '8': [[' ### '], [' # # '], ['# #'], [' ### '], ['# #'], [' # # '], [' ### ']], '9': [[' ### '], [' # # '], ['# #'], [' #### '], [' # '], [' # # '], [' ### ']] } # 打印时钟的每一行 for i in range(7): print(' '.join([digits[digit][i][0] for digit in current_time])) # 每秒更新时钟显示 while True: display_time() time.sleep(1) print('\n' * 100) # 清空屏幕 ``` 以上代码使用了字典`digits`来存储每个数字的显示模式,每个数字由7行组成。`display_time`函数获取当前时间并打印每个数字的每一行,以形成数字时钟的效果。通过不断地调用`display_time`并使用`time.sleep`函数来每秒刷新时钟显示。 ### 回答3: 实现模拟数字时钟的Python代码可以使用turtle模块来绘制时钟的外框和数字,使用time模块来获取系统时间,并利用递归函数来实现时钟的动态更新。 以下是一个实现模拟数字时钟的Python代码示例: ```python import turtle import time # 设置窗口大小和画笔速度 win = turtle.Screen() win.setup(400, 300) win.tracer(0) # 绘制数字时钟外框 def draw_frame(): pen = turtle.Turtle() pen.penup() pen.goto(-75, 100) pen.pendown() for _ in range(2): pen.forward(150) pen.right(90) pen.forward(200) pen.right(90) pen.hideturtle() # 绘制数字 def draw_digits(): digits = [[(-20, 60), (-20, 40), (20, 40), (20, 60), (-20, 60)], # 0 [(0, 60), (0, 40)], # 1 [(-20, 60), (20, 60), (20, 50), (-20, 50), (-20, 40), (20, 40)], # 2 [(-20, 60), (20, 60), (20, 50), (-20, 50), (20, 40), (-20, 40)], # 3 [(-20, 60), (-20, 50), (20, 50), (20, 60), (20, 40)], # 4 [(20, 60), (-20, 60), (-20, 50), (20, 50), (20, 40), (-20, 40)], # 5 [(-20, 60), (-20, 50), (20, 50), (20, 60), (-20, 60), (-20, 40), (20, 40)], # 6 [(20, 60), (-20, 60), (-20, 40)], # 7 [(-20, 60), (-20, 40), (20, 40), (20, 60), (-20, 60), (20, 40)], # 8 [(-20, 50), (20, 50), (20, 60), (-20, 60), (-20, 40), (20, 40)]] # 9 pen = turtle.Turtle() pen.penup() pen.pensize(5) for i in range(10): pen.goto(digits[i][0]) pen.pendown() for j in range(1, len(digits[i])): pen.goto(digits[i][j]) pen.penup() pen.hideturtle() # 更新时钟时间 def update_clock(): current_time = time.strftime("%H:%M:%S") pen = turtle.Turtle() pen.penup() pen.goto(-70, 50) pen.clear() pen.write(current_time, align="center", font=("Courier", 24, "normal")) win.update() pen.hideturtle() win.ontimer(update_clock, 1000) # 每秒更新一次 # 主函数 def main(): draw_frame() draw_digits() update_clock() turtle.done() if __name__ == "__main__": main() ``` 此代码使用了turtle模块来绘制数字时钟的外框和数字,并利用time模块来获取当前时间。通过使用递归函数和`win.ontimer()`方法,每隔1秒钟更新一次时钟时间,并实时显示在窗口中。 注意:为了使程序运行,需要确保已经安装了turtle库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值