Exemple 1 : subtract 2 dates
from datetime import datetime, date, timedelta date_1 = datetime.strptime('2018-06-01', "%Y-%m-%d") date_2 = datetime.strptime('2018-06-04', "%Y-%m-%d") diff_date = date_2 - date_1 print(diff_date.days)
run the command.
python3 -m script script.py root@numberspeaks.com:~#3
Exemple 2 : add or subtract days to a date
from datetime import datetime, date, timedelta date_1 = datetime.strptime('2018-06-01', "%Y-%m-%d") new_date = date_1 + timedelta(days=4) print(new_date)
run the command.
python3 -m script script.py root@numberspeaks.com:~#2018-06-05 00:00:00
Exemple 3 : today date
from datetime import datetime, date, timedelta today_date = datetime.today()
Exemple 4 : convert string to datetime
from datetime import datetime, date, timedelta date = datetime.strptime('2018-06-01', "%Y-%m-%d")
Exemple 5 : convert datetime to string
from datetime import datetime, date, timedelta date = datetime.today().strftime("%Y-%m-%d")