Python 关于下班倒计时,我的写法如下

文章介绍了如何使用Python编程实现一个定制化的倒计时函数,包括下班铃声、周五下班、中秋节和除夕等特定时间点的计时提醒。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于下班倒计时,我的写法如下

from datetime import datetime

def printCountDown(tag, now, year,month, day, hour, min, seconds):
    thatDay = datetime(year,month,day,hour,min,seconds)
    diff = thatDay.timestamp() - now.timestamp()
    
    seconds = diff 
    month = 0
    hour = 0
    min = 0
    day = 0
    if seconds > 60:
        min = int(seconds / 60)
        seconds = int(seconds % 60)
        
    if min > 60:
        hour = int(min / int(60))
        min = int(min % int(60))
        

    if hour > 24:
        day = int(hour / int(24))
        hour = int(hour % int(24))
        

    if day > 30:
        month = int(day / int(30))
        day = int(day % int(30))


    if month > 0:
        print("\t距离 {} 还有 {}个月{}天{}小时{}分{}秒".format(tag,month,day,hour,min,seconds))
    elif day > 0:
        print("\t距离 {} 还有 {}天{}小时{}分{}秒".format(tag,day,hour,min,seconds))
    elif hour > 0:
        print("\t距离 {} 还有 {}小时{}分{}秒".format(tag,hour,min,seconds))
    elif min > 0:
        print("\t距离 {} 还有 {}分{}秒".format(tag,min,seconds))  
    elif seconds >0:
        print("\t距离 {} 还有 {}秒".format(tag,seconds)) 
    else:
        print("\t{} 已到".format(tag)) 
    pass

now = datetime.now()

print("温馨提示:")
printCountDown(tag="下班铃声响", now=now, year=now.year, month=now.month, day=now.day, hour=18, min=33, seconds=0)
weekday = now.weekday()

if weekday > 4:
    fridayOffset = 6 - weekday + 5
else:
    fridayOffset = 4 - weekday
# 0 1 2 3 4 5 6

friday = datetime(now.year,now.month,now.day + fridayOffset, now.hour,now.minute,now.second)
if fridayOffset < 5:
    printCountDown(tag="周五下班", now=now, year=now.year, month=now.month, day=now.day + fridayOffset, hour=18, min=33, seconds=0)
printCountDown(tag="中秋节", now=now, year=2023, month=9, day=28, hour=18, min=33, seconds=0)
printCountDown(tag="除夕", now=now, year=2024, month=2, day=8, hour=18, min=33, seconds=0)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值