Python中datetime包

1.问题一

1.0 问题描述

想要进行日期类型的计算,获取一些日期数据,使用Python中的datetime包相关API

1.1 解决

导包,获取datetime时间类型数据,获取weekday星期,获取日期插值,均可以十分简单的解决

from datetime import *
dt1 = datetime(1901,1,1)
dt2 = datetime(2000,12,31)
print(dt1.weekday())
td = dt2 - dt1
print(td.days//7)

2. 问题二

2.0 问题描述

2.1 题解

使用datetime包中的weekday和timedelta函数(时间差)即可轻松完成

from datetime import *
start_date = datetime(2024,4,6)
end_date = start_date + timedelta(days=20**22%7)
print(end_date.weekday() + 1)

3. 问题三

3.0 问题描述

3.1 题解

from datetime import *
start_time = datetime(2000,1,1)
end_time = datetime(2020,10,1)
curr_time = start_time
ans = 0
while curr_time <= end_time:
    if curr_time.weekday() == 0 or curr_time.day == 1: 
        ans += 2
    else:
        ans += 1
    curr_time += timedelta(days = 1)
differ_date = end_time - start_time
print(differ_date.days)
print(ans)
posted @ 2024-04-03 15:35  DawnTraveler  阅读(28)  评论(0编辑  收藏  举报