其他分享
首页 > 其他分享> > datetime一些类型

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

 

标签:12,timedelta,datetime,print,2021,类型,一些,now
来源: https://www.cnblogs.com/nicole-zhang/p/15247975.html