Course Content
Dealing with Dates and Times in Python
Dealing with Dates and Times in Python
Date Difference
What if we want to calculate the difference between two dates? Surely, we can do it by hand, but there are months and years with a different number of days (either 30, either 31, or even 28 or 29 for months and 365 or 366 for years).
There is timedelta
class in the datetime
library, which object represents a duration, the difference between two dates or times. timedelta
object has 7 arguments, all optional (days, seconds, microseconds, milliseconds, minutes, hours, weeks). Since we are working with dates only, we will focus on only days
argument. Note, that argument weeks
converts into 7 days. By default, the difference between two dates/times will be represented as the number of days, hours, minutes, and seconds. Since we are comparing dates, all the arguments but days will be 0. For example, the WHO declared the COVID-19 pandemic on 11 March 2020. We can count the number of days since this event till today.
# Load classes from library from datetime import date # Create two date objects pandemic = date(2020, 3, 11) today = date.today() # Count number of days print(today - pandemic, "passed since COVID-19 became pandemic")
Now it's your turn!
Task
Create two date
objects date1
and date2
with dates "14 January 2016" and "07 April 2019" respectively. Find the difference between these two dates.
Thanks for your feedback!
Date Difference
What if we want to calculate the difference between two dates? Surely, we can do it by hand, but there are months and years with a different number of days (either 30, either 31, or even 28 or 29 for months and 365 or 366 for years).
There is timedelta
class in the datetime
library, which object represents a duration, the difference between two dates or times. timedelta
object has 7 arguments, all optional (days, seconds, microseconds, milliseconds, minutes, hours, weeks). Since we are working with dates only, we will focus on only days
argument. Note, that argument weeks
converts into 7 days. By default, the difference between two dates/times will be represented as the number of days, hours, minutes, and seconds. Since we are comparing dates, all the arguments but days will be 0. For example, the WHO declared the COVID-19 pandemic on 11 March 2020. We can count the number of days since this event till today.
# Load classes from library from datetime import date # Create two date objects pandemic = date(2020, 3, 11) today = date.today() # Count number of days print(today - pandemic, "passed since COVID-19 became pandemic")
Now it's your turn!
Task
Create two date
objects date1
and date2
with dates "14 January 2016" and "07 April 2019" respectively. Find the difference between these two dates.
Thanks for your feedback!
Date Difference
What if we want to calculate the difference between two dates? Surely, we can do it by hand, but there are months and years with a different number of days (either 30, either 31, or even 28 or 29 for months and 365 or 366 for years).
There is timedelta
class in the datetime
library, which object represents a duration, the difference between two dates or times. timedelta
object has 7 arguments, all optional (days, seconds, microseconds, milliseconds, minutes, hours, weeks). Since we are working with dates only, we will focus on only days
argument. Note, that argument weeks
converts into 7 days. By default, the difference between two dates/times will be represented as the number of days, hours, minutes, and seconds. Since we are comparing dates, all the arguments but days will be 0. For example, the WHO declared the COVID-19 pandemic on 11 March 2020. We can count the number of days since this event till today.
# Load classes from library from datetime import date # Create two date objects pandemic = date(2020, 3, 11) today = date.today() # Count number of days print(today - pandemic, "passed since COVID-19 became pandemic")
Now it's your turn!
Task
Create two date
objects date1
and date2
with dates "14 January 2016" and "07 April 2019" respectively. Find the difference between these two dates.
Thanks for your feedback!
What if we want to calculate the difference between two dates? Surely, we can do it by hand, but there are months and years with a different number of days (either 30, either 31, or even 28 or 29 for months and 365 or 366 for years).
There is timedelta
class in the datetime
library, which object represents a duration, the difference between two dates or times. timedelta
object has 7 arguments, all optional (days, seconds, microseconds, milliseconds, minutes, hours, weeks). Since we are working with dates only, we will focus on only days
argument. Note, that argument weeks
converts into 7 days. By default, the difference between two dates/times will be represented as the number of days, hours, minutes, and seconds. Since we are comparing dates, all the arguments but days will be 0. For example, the WHO declared the COVID-19 pandemic on 11 March 2020. We can count the number of days since this event till today.
# Load classes from library from datetime import date # Create two date objects pandemic = date(2020, 3, 11) today = date.today() # Count number of days print(today - pandemic, "passed since COVID-19 became pandemic")
Now it's your turn!
Task
Create two date
objects date1
and date2
with dates "14 January 2016" and "07 April 2019" respectively. Find the difference between these two dates.