Library contents

Python timedelta.days

Subtract the arrival date from the departure date to count nights booked.

A simple example

timedelta.days

  1. Start with Jan 1 and Jan 3 of the same year
  2. Do this Count elapsed days
  3. Result 2 days
The two steps are January 1 to 2, then January 2 to 3.

Syntax

Python syntax
(date.fromisoformat("2024-03-01") - arrival).days
Run these examples in Python

Save one example at a time as lesson.py. Use Python 3.9 or later.

python3 lesson.py

Count nights in a leap-year hotel stay

A reservation spans the end of February in a leap year. The booking system must use actual calendar days so that the extra day appears in the schedule or stay length.

Result2
lesson.py
from datetime import date, timedelta

arrival = date.fromisoformat("2024-02-28")
print((date.fromisoformat("2024-03-01") - arrival).days)
Output
2

A guest arriving on February 28 and leaving on March 1 stays the nights of February 28 and February 29.

Advertisements

Count a winter cabin stay

A family arrives on December 30 and leaves on January 2. Count the number of midnights from the start date up to the end date, using local dates.

Given30 Dec 2026 → 2 Jan 2027
Worked result3

Close a same-day hire

A visitor returns a borrowed projector on the day it was collected. Count the number of midnights from the start date up to the end date, using local dates.

Given12 May → 12 May
Worked result0

What to watch for

The checkout date is excluded. For calendar dates, avoid using the length of a local daylight-saving day as a constant.

Documentation for timedelta.days