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

Conteúdo do 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

book
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!

Tarefa

Swipe to start coding

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

Solução

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 7
toggle bottom row

book
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!

Tarefa

Swipe to start coding

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

Solução

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 7
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Sentimos muito que algo saiu errado. O que aconteceu?
some-alt