实验代码:
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import datetime
>>> delta = datetime.timedelta(days=10, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
>>> delta.days
10
>>> delta.seconds
0
>>> delta.min
datetime.timedelta(days=-999999999)
>>> delta.max
datetime.timedelta(days=999999999, seconds=86399, microseconds=999999)
>>> delta.resolution
datetime.timedelta(microseconds=1)
>>>
>>>
>>> # 代表时间间隔的类
>>> d1 = datetime.timedelta(days=12)
>>> d2 = datetime.timedelta(days=5)
>>> d1,d2
(datetime.timedelta(days=12), datetime.timedelta(days=5))
>>> d1
datetime.timedelta(days=12)
>>> d2
datetime.timedelta(days=5)
>>> d1+d2
datetime.timedelta(days=17)
>>> d1-d2
datetime.timedelta(days=7)
>>> d2-d1
datetime.timedelta(days=-7)
>>> d1*0.5
datetime.timedelta(days=6)
>>> 0.5*d1
datetime.timedelta(days=6)
>>> 0.6*d1
datetime.timedelta(days=7, seconds=17280)
>>> 0.2*24*60*60
17280.000000000004
>>> 12*0.6
7.199999999999999
>>> d1*0.6
datetime.timedelta(days=7, seconds=17280)
>>> 0.6*d1
datetime.timedelta(days=7, seconds=17280)
>>> d1/d2
2.4
>>> d2/d1
0.4166666666666667
>>> str(d1)
'12 days, 0:00:00'
>>> repr(d1)
'datetime.timedelta(days=12)'
>>> d1>d2
True
>>> d1>=d2
True
>>> d1==d2
False
>>> d1<d2
False
>>> d1<=d2
False
>>> datetime.date.today()
datetime.date(2022, 10, 16)
>>>
>>> #代表日期的类
>>> datetime.date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)
>>> datetime.date(2022, 12, 31)
datetime.date(2022, 12, 31)
>>> datetime.date.fromisoformat('2019-01-03')
datetime.date(2019, 1, 3)
>>> datetime.date(2002, 12, 4).isoformat()
'2002-12-04'
>>>
>>>
>>> datetime.date.min
datetime.date(1, 1, 1)
>>> datetime.date.max
datetime.date(9999, 12, 31)
>>> datetime.date.resolution
datetime.timedelta(days=1)
>>> datetime.date.year
<attribute 'year' of 'datetime.date' objects>
>>> datetime.date.month
<attribute 'month' of 'datetime.date' objects>
>>> datetime.date.day
<attribute 'day' of 'datetime.date' objects>
>>>
>>> d1 = datetime.date.fromisoformat('2022-01-05')
>>> d2 = datetime.date.fromisoformat('2022-03-09')
>>> d1-d2
datetime.timedelta(days=-63)
>>> d1-d1
datetime.timedelta(0)
>>> d2-d1
datetime.timedelta(days=63)
>>> d1.min
datetime.date(1, 1, 1)
>>> d1.max
datetime.date(9999, 12, 31)
>>> d1.resolution
datetime.timedelta(days=1)
>>> d1.year
2022
>>> d1.month
1
>>> d1.day
5
>>>
>>>
>>>
>>> timedelta = datetime.timedelta(days=12)
>>> timedelta
datetime.timedelta(days=12)
>>> d1
datetime.date(2022, 1, 5)
>>> d1+timedelta
datetime.date(2022, 1, 17)
>>> d1-timedelta
datetime.date(2021, 12, 24)
>>> timedelta = datetime.timedelta(days=2)
>>> d1
datetime.date(2022, 1, 5)
>>> d1+timedelta
datetime.date(2022, 1, 7)
>>> d1-timedelta
datetime.date(2022, 1, 3)
>>> d1+datetime.timedelta(days=365)
datetime.date(2023, 1, 5)
>>> d1
datetime.date(2022, 1, 5)
>>> d2
datetime.date(2022, 3, 9)
>>> d1<d2
True
>>> d1<=d2
True
>>> d1==d2
False
>>> d1>=d2
False
>>> d1>d2
False
>>> d3 = datetime.date(2022, 1, 5)
>>> d3
datetime.date(2022, 1, 5)
>>> d1
datetime.date(2022, 1, 5)
>>> d1==d3
True
>>> d1>=d3
True
>>> d1.toordinal()
738160
>>> d1
datetime.date(2022, 1, 5)
>>> d1.weekday()#返回一个整数代表星期几,星期一为0,星期天为6。
2
>>> d1.isoweekday()
3
>>> d1.isoweekday()# 返回一个整数代表星期几,星期一为1,星期天为7。
3
>>> d1.isocalendar()
(2022, 1, 3)
>>> d1
datetime.date(2022, 1, 5)
>>> d1,d1.isocalendar()
(datetime.date(2022, 1, 5), (2022, 1, 3))
>>>
>>>
>>> d1
datetime.date(2022, 1, 5)
>>> d1.isoformat()
'2022-01-05'
>>> str(d1)
'2022-01-05'
>>> repr(d1)
'datetime.date(2022, 1, 5)'
>>> d1
datetime.date(2022, 1, 5)
>>>
>>>