Operations with Dates
Now that you have understood what datetime
objects are and how you can transform them, we can introduce the timedelta()
function. timedelta()
allows you to alter your dates by adding a delta to your dates (days, months, ecc..). We will also show how easy it is to find the difference between two dates.
Uppgift
Swipe to start coding
- Import the
datetime
library with thetimedelta
module; - Find the difference between two dates;
- Add 30 days to the
date1
variable.
Lösning
9
1
2
3
4
5
6
7
8
9
from datetime import datetime, timedelta
date1 = datetime(2022, 1, 8)
date2 = datetime(2022, 1, 12)
difference = date2 - date1
print(difference)
date1 += timedelta(days = 30)
print(date1)
Mark tasks as Completed
Var allt tydligt?
Tack för dina kommentarer!
Avsnitt 1. Kapitel 5
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal