Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Date Difference | Working with Dates
Dealing with Dates and Times in Python
course content

Course Content

Dealing with Dates and Times in Python

Dealing with Dates and Times in Python

1. Working with Dates
2. Working with Times
3. Timezones and Daylight Savings Time (DST)
4. Working with Dates and Times in pandas

bookDate 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.

123456789
# 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")
copy

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 7
toggle bottom row

bookDate 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.

123456789
# 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")
copy

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 7
toggle bottom row

bookDate 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.

123456789
# 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")
copy

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

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.

123456789
# 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")
copy

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 1. Chapter 7
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt