1. 天数差(datetime、parse、rrule)
import datetime
from dateutil import parser
from dateutil import rrule
d1 = '2022-07-26'
d2 = '2022-08-01'
date1 = datetime.datetime.strptime(d1, "%Y-%m-%d").date()
date2 = datetime.datetime.strptime(d2, "%Y-%m-%d").date()
a1 = (date2 - date1).days
print("a1", a1)
a2 = (parser.parse(d2) - parser.parse(d1)).days
print("a2", a2)
a3 = rrule.rrule(rrule.DAILY, dtstart=date1, until=date2).count()
print("a3", a3)
date1 = datetime.datetime(2022, 7, 26, 15, 35, 10)
date2 = datetime.datetime(2022, 8, 1, 18, 30, 20)
b1 = (date2 - date1).days
print("b1", b1)
b2 = (parser.parse(d2) - parser.parse(d1)).days
print("b2", b2)
b3 = rrule.rrule(rrule.DAILY, dtstart=date1, until=date2).count()
print("b3", b3)
E:\pythonProject\venv\Scripts\python.exe E:/pythonProject/csdn_project/001.py
a1 6
a2 6
a3 7
b1 6
b2 6
b3 7
2. 秒数差(seconds、microseconds)
Seconds = (date2 - date1).seconds
s1= (date2 - date1).microseconds
3. 周数差(rrule)
Weeks = rrule.rrule(rrule.WEEKLY, dtstart = date1, until = date2).count()
4. 月数差(rrule)
Months = rrule.rrule(rrule.MONTHLY, dtstart = date1, until = date2).count()
5. 年数差
Years = rrule.rrule(rrule.YEARLY, dtstart = date1, until = date2).count()