Dates

Dates

The datetime module handles dates and times. Its date type represents a calendar day:

from datetime import date

release = date(2026, 7, 15)
print(release.year)
print(release.isoformat())

Output:

2026
2026-07-15

This from datetime import date form imports one name directly, so you write date(...) instead of datetime.date(...). Both forms are common.

Subtracting two dates gives the distance between them; its days attribute is a plain number:

start = date(2026, 7, 1)
end = date(2026, 7, 15)
gap = end - start
print(gap.days)

Output:

14

date.today() returns the current date. Dates compare naturally: start < end is True.

Exercise

  1. Import date from datetime.
  2. Define a function named days_between with two parameters, start and end — both date values. It returns the number of days from start to end.
  3. Define a function named is_past_deadline with two parameters, deadline and today — both date values. It returns True when today is after deadline.

days_between(date(2026, 7, 1), date(2026, 7, 15)) returns 14.

Run your code to see the output, then press Submit.

Tests

import unittest
from datetime import date


class TestDates(unittest.TestCase):
    def test_two_weeks_apart(self):
        self.assertEqual(days_between(date(2026, 7, 1), date(2026, 7, 15)), 14)

    def test_same_day_is_zero(self):
        self.assertEqual(days_between(date(2026, 1, 1), date(2026, 1, 1)), 0)

    def test_across_a_month_boundary(self):
        self.assertEqual(days_between(date(2026, 1, 30), date(2026, 2, 2)), 3)

    def test_day_after_deadline_is_past(self):
        self.assertIs(is_past_deadline(date(2026, 7, 1), date(2026, 7, 2)), True)

    def test_deadline_day_itself_is_not_past(self):
        self.assertIs(is_past_deadline(date(2026, 7, 1), date(2026, 7, 1)), False)
from datetime import date


def days_between(start, end):
    return (end - start).days


def is_past_deadline(deadline, today):
    return today > deadline


print(days_between(date(2026, 7, 1), date(2026, 7, 15)))
print(is_past_deadline(date(2026, 7, 1), date(2026, 7, 15)))
Solution hidden. Give it a real try first.

Sign in to join the discussion.

No comments yet. Be the first to ask a question.

main.py
Console
Press Run to execute your code, or Submit to test and complete this problem.