当前位置:   article > 正文

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

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

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

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/278909?site
推荐阅读
相关标签
  

闽ICP备14008679号