Feb-22-2018, 07:22 PM
The following from: https://pymotw.com/3/datetime/
Date math uses the standard arithmetic operators.
datetime_date_math.py
Date math uses the standard arithmetic operators.
datetime_date_math.py
import datetime today = datetime.date.today() print('Today :', today) one_day = datetime.timedelta(days=1) print('One day :', one_day) yesterday = today - one_day print('Yesterday:', yesterday) tomorrow = today + one_day print('Tomorrow :', tomorrow) print() print('tomorrow - yesterday:', tomorrow - yesterday) print('yesterday - tomorrow:', yesterday - tomorrow)This example with date objects illustrates using timedelta objects to compute new dates, and subtracting date instances to produce timedeltas (including a negative delta value).
Output:$ python3 datetime_date_math.py
Today : 2017-07-30
One day : 1 day, 0:00:00
Yesterday: 2017-07-29
Tomorrow : 2017-07-31
tomorrow - yesterday: 2 days, 0:00:00
yesterday - tomorrow: -2 days, 0:00:00