1.计算时间差
两个时间差相减 .days属性
>>> str = datetime.strptime('2019-08-08', '%Y-%m-%d')
>>> t = datetime.strptime('2019-09-02', '%Y-%m-%d')
>>> d = (t-str).days
>>> d
25
2.获取当月的第一天和最后一天
import calendar
def _getMonthFirstDayAndLastDay(self, year=None, month=None):
"""
Get the first and last day of the specified month
"""
firstDayWeekDay, monthRange = calendar.monthrange(year, month)
firstDay = datetime(year=year, month=month, day=1)
lastDay = datetime(year=year, month=month, day=monthRange)
return firstDay, lastDay