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

Sveip for å vise menyen

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!

Oppgave

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.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 7
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

close

Awesome!

Completion rate improved to 3.23

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!

Oppgave

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.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

close

Awesome!

Completion rate improved to 3.23

Sveip for å vise menyen

some-alt