赞
踩
关于下班倒计时,我的写法如下
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)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。