美文网首页
datetime 时间计算

datetime 时间计算

作者: 看不见上帝 | 来源:发表于2020-07-16 19:11 被阅读0次

计算某个时间距离当天还有多少天有几个周日

from datetime import datetime,timedelta
thatDay = "2020-10-01"
theDay = datetime.strptime(thatDay, "%Y-%m-%d")             #XX日期转换
today = datetime.now()                                      #当前日期
print(f"离{thatDay}还有{(theDay - today).days}天")          #剩余日期
a = 0
for i in range((theDay - today).days):
    thatDay = today + timedelta(days=i)
    if thatDay.weekday() == 6:
        # print(thatDay.strftime('%Y-%m-%d'))               #出现周天日期
        a += 1                                              #周天次数
print(f"有{a}个周天,可以放{a+a/2:.0F}天假")

相关文章

网友评论

      本文标题:datetime 时间计算

      本文链接:https://www.haomeiwen.com/subject/zghmhktx.html