Python Pendulum: Create and Convert DateTimes

Working with dates and times in Python means deciding how to handle timezone information. Pendulum is a Python library for creating timezone-aware date and time values and converting an instant between zones.

I’ll show you how to create a DateTime in a named zone and convert it to another timezone.

TL;DR

Pendulum adds timezone-aware date and time objects, calendar arithmetic, parsing, and readable duration helpers to Python.

  • Use the installation command shown in Step 1.
  • Create a value in a named IANA timezone for local calendar work.
  • Convert an existing instant with its timezone conversion method.

What is the Python Pendulum module?

Pendulum is a third-party Python library for dates, times, timezones, and durations. The additional helpers cover parsing and timezone transitions.

Python’s datetime module handles many tasks without an extra dependency. The project documentation also warns that libraries checking exact object types can need compatibility testing.

PyPI lists Python 3.10 and newer for Pendulum 3.3.0. Check package metadata before choosing an interpreter.

How to use Pendulum step by step

Create the DateTime in the zone where its clock reading belongs. You can then convert that instant or do arithmetic in the original zone.

Step 1: Install Pendulum and import it

Run the install command in the active Python environment. The package and import both use the lowercase name pendulum.

python3 -m pip install pendulum

The installation output lists Pendulum and its timezone data package:

Successfully installed pendulum-3.3.0 tzdata-2026.6

Import the module in a Python file. If Python reports a missing module, check that the script interpreter is the same environment where pip installed it.

import pendulum

Step 2: Create a DateTime in a named zone

Use an IANA timezone name to make the intended region explicit. This example adds one calendar day across the 2024 spring transition in New York.

import pendulum

start = pendulum.datetime(2024, 3, 9, 12, tz="America/New_York")
next_day = start.add(days=1)
print(start.to_iso8601_string())
print(next_day.to_iso8601_string())
print(next_day.diff_for_humans(start))

Save the example as pendulum_demo.py and run python3 pendulum_demo.py.

2024-03-09T12:00:00-05:00
2024-03-10T12:00:00-04:00
1 day after
Terminal output showing Pendulum offsets before and after adding one day
The local clock stays at noon while the UTC offset changes.

I ran this with Pendulum 3.3.0. The offset changes from −05:00 to −04:00.

Step 3: Convert the same instant to another timezone

Timezone conversion changes the displayed zone for an instant. The clock time and offset can both change.

london_time = start.in_timezone("Europe/London")
print(london_time.to_iso8601_string())

For a current value, pass a zone to pendulum.now(). A value created with pendulum.datetime() defaults to UTC, while now() uses the local zone unless one is passed. I pass the zone explicitly because it makes the intended region visible in the call.

Step 4: Add calendar time and inspect a duration

The add() and subtract() methods take calendar units such as days or months. A calendar month is not a fixed number of days, so choose the operation that matches the rule your application needs. I use days for the demonstrated next-calendar-day task.

Subtracting DateTime values gives an interval that can be expressed in calendar days or elapsed hours.

Use ISO 8601 formatting when the numeric offset matters. A date-only format suits calendar labels, but omitting the offset can hide which instant an event timestamp represents.

Timezone and date edge cases

A local clock reading can be skipped or repeated when timezone rules change. Pendulum can normalize an invalid clock reading or reject it. Choose based on whether a caller can confirm the intended time.

CaseSymptomFix
Skipped local time in a spring transitionThe requested wall-clock time does not exist, so default construction normalizes it to a valid time.Accept the normalized local time, or pass raise_on_unknown_times=True to raise NonExistingTime.
Repeated local time in an autumn transitionThe same clock reading maps to two possible instants. By default, Pendulum selects the post-transition occurrence.Pass fold=0 for the first occurrence, or raise_on_unknown_times=True to raise AmbiguousTime.
Convert an existing instantChanging timezone assignment can be confused with displaying one instant in another region.Use in_timezone() for conversion, then inspect the zone and ISO offset.
Add a month at month endCalendar arithmetic reaches a month with fewer days than the starting month.Test the exact boundary dates and define the desired application rule.
Mix with native datetime codeA library checking exact object types may not accept a subclass instance as expected.Test that integration and convert at its boundary if it requires a native datetime type.

For conversion from a local zone to UTC, create the value with its correct source zone before converting. A bare clock reading does not identify a unique instant.

Conclusion

Choose an explicit timezone when local calendar meaning matters, and inspect the offset for event timestamps. Use the project’s Pendulum documentation and repository for API details.

For related Python timezone work, read AskPython’s datetime module guide and pytz to zoneinfo guide.

FAQ

Check the installation command and decide how to handle dates whose timezone is missing or ambiguous.

How do I install Pendulum in Python?

Run python3 -m pip install pendulum in the environment used by your script, then import the pendulum module.

Does Pendulum use UTC by default?

pendulum.datetime() defaults to UTC when no zone is supplied. pendulum.now() uses the local zone unless you pass one.

How does Pendulum handle ambiguous local times?

By default, Pendulum normalizes nonexistent times and selects the post-transition occurrence for an ambiguous time. Pass fold=0 for the first occurrence or raise_on_unknown_times=True to reject ambiguity.

Safa Mulani
Safa Mulani

An enthusiast in every forthcoming wonders!

Articles: 189