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

Contenido del Curso

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

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.

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!

Tarea

Create two date objects date1 and date2 with dates "14 January 2016" and "07 April 2019" respectively. Find the difference between these two dates.

Tarea

Create two date objects date1 and date2 with dates "14 January 2016" and "07 April 2019" respectively. Find the difference between these two dates.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 1. Capítulo 7
toggle bottom row

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.

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!

Tarea

Create two date objects date1 and date2 with dates "14 January 2016" and "07 April 2019" respectively. Find the difference between these two dates.

Tarea

Create two date objects date1 and date2 with dates "14 January 2016" and "07 April 2019" respectively. Find the difference between these two dates.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 1. Capítulo 7
toggle bottom row

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.

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!

Tarea

Create two date objects date1 and date2 with dates "14 January 2016" and "07 April 2019" respectively. Find the difference between these two dates.

Tarea

Create two date objects date1 and date2 with dates "14 January 2016" and "07 April 2019" respectively. Find the difference between these two dates.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

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!

Tarea

Create two date objects date1 and date2 with dates "14 January 2016" and "07 April 2019" respectively. Find the difference between these two dates.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 1. Capítulo 7
Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt