Python Practice › Basic Programs › Program 22

Days between two dates

EasyBasic Programs · 22 of 40

Write a Python program to calculate the number of days between two dates.

Advertisement
Input
(2023, 1, 5) and (2023, 1, 22)
Output
17 days

Approach

  1. Create two date objects.
  2. Subtract them to get a timedelta and read its .days.

Solution

Python
from datetime import date start = date(2023, 1, 5)end = date(2023, 1, 22)gap = end - startprint(gap.days, "days")
Output
17 days

Line by line

Line 5

Subtracting dates gives a timedelta object.

Line 6

.days is the whole number of days between them.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More basic programs

← All 40 Basic Programs