datetime一些类型

import datetime

datetime.date(2021,12,1)
# 2021-12-01
datetime.time(12,23,48,333)
# 12:23:48.000333
datetime.datetime(2021,12,1,12,23,48,333)
# 2021-12-01 12:23:48.000333
datetime.datetime.now()
# 2021-09-09 17:17:53.067093
datetime.timedelta(12,3600)
# 12 days, 1:00:00


now = datetime.datetime.now()
print(now) # 2021-09-08 11:30:35.577465
print(now.year) # 2021
print(now.month) # 9
print(now.day) # 8
print(now.hour) # 11
print(now.minute) # 30
print(now.second) # 35
print(now.microsecond) # 577465

# 时间差 timedelta
delta = datetime.datetime(2021,4,16,1,2,3) - datetime.datetime(2021,4,10,0,0,0)
print(delta) # 6 days, 1:02:03
print(delta.days) # 6
print(delta.seconds) # 3723

delta2 = datetime.timedelta(926,56700)
print(delta2.days) # 926
print(delta2.seconds) # 56700

# 可以给datetime加上(或减去)一个或多个delta
start = datetime.datetime(2021,1,1)
new = start + datetime.timedelta(12) - 2*datetime.timedelta(1,3600)
print(new) # 2021-01-10 22:00:00

 

上一篇:Codeforces 1082D Maximum Diameter Graph (贪心构造)


下一篇:【LeetCode】974. 和可被 K 整除的子数组