Top 10 Python Date/Time Libraries with Practical Code Samples
This guide presents ten Python date and time libraries—Arrow, Chronyk, dateutil, Delorean, Maya, Pendulum, PyTime, pytz, when.py, and moment—detailing typical scenarios, core features, and ready-to-run code snippets for creation, parsing, formatting, timezone conversion, and arithmetic.
1. Arrow
Scenario: Use Arrow for creating, formatting, and converting date and time across time zones.
import arrow
# Create current time
now = arrow.now()
print("Now (local):", now)
# Format output
formatted_now = now.format('YYYY-MM-DD HH:mm:ss ZZ')
print("Formatted Now:", formatted_now)
# Timezone conversion
utc_time = now.to('UTC')
print("UTC Time:", utc_time)
# Date arithmetic
one_week_later = now.shift(weeks=+1)
print("One Week Later:", one_week_later)2. Chronyk
Scenario: Parse handwritten date and time expressions with Chronyk.
from chronyk import Chronyk, ChronykDelta
# Parse a natural language time
t = Chronyk("next Monday at 3pm")
print("Parsed Time:", t.get())
# Compute difference between two moments
t2 = Chronyk("last Friday at 6am")
delta = ChronykDelta(t - t2)
print("Time Difference (days):", delta.days())3. dateutil
Scenario: Use dateutil to parse relative date strings and perform timezone conversion.
from dateutil import parser, tz
# Parse a relative date string
dt = parser.parse("yesterday 10:00 PM")
print("Parsed Relative Date:", dt)
# Timezone conversion
local_tz = tz.gettz('America/New_York')
localized_dt = dt.astimezone(local_tz)
print("Localized Date and Time:", localized_dt)4. Delorean
Scenario: Perform date and time operations, including timezone shifts and date calculations, with Delorean.
from delorean import Delorean, parse
# Create a Delorean object (current time)
d = Delorean()
# Timezone shift
d = d.shift('US/Eastern')
print("Eastern Time:", d.datetime)
# Date calculation
one_month_ago = d.last_month()
print("One Month Ago:", one_month_ago.datetime)5. Maya
Scenario: Human‑friendly time handling—parsing, formatting, and timezone conversion—using Maya.
import maya
# Current local time
now = maya.now()
print("Now (local):", now)
# Convert to UTC
utc_time = now.datetime(to_timezone='UTC')
print("UTC Time:", utc_time)
# Parse natural language time
parsed_time = maya.when("next Thursday at 8:00 AM")
print("Parsed Time:", parsed_time)
# Compute difference
time_diff = maya.now() - parsed_time
print("Time Difference (in days):", time_diff.days)6. Pendulum
Scenario: Precise date/time manipulation, including timezone conversion and arithmetic, with Pendulum.
import pendulum
# Create current time
now = pendulum.now()
print("Now (local):", now)
# Timezone conversion
utc_time = now.in_tz('UTC')
print("UTC Time:", utc_time)
# Date arithmetic
one_week_later = now.add(weeks=1)
print("One Week Later:", one_week_later)
# Format output
formatted_time = one_week_later.format('YYYY-MM-DD HH:mm:ss ZZ')
print("Formatted Time:", formatted_time)7. PyTime
Scenario: Create and manipulate dates/times from strings using PyTime.
from pytime import pytime
# Create time from string
time_str = "2024-10-15 15:10"
dt = pytime.parse(time_str)
print("Parsed Time:", dt)
# Date calculation
one_day_later = pytime.add(dt, days=1)
print("One Day Later:", one_day_later)
# Format output
formatted_time = pytime.strftime(one_day_later, "%Y-%m-%d %H:%M:%S")
print("Formatted Time:", formatted_time)8. pytz
Scenario: Cross‑platform timezone calculations using pytz.
import datetime
import pytz
# Create a timezone‑aware datetime (UTC)
utc_dt = datetime.datetime.now(pytz.utc)
print("UTC Time:", utc_dt)
# Convert to another timezone
eastern = pytz.timezone('US/Eastern')
eastern_dt = utc_dt.astimezone(eastern)
print("Eastern Time:", eastern_dt)
# List available timezones (show first five)
all_timezones = pytz.all_timezones
print("All Timezones:", all_timezones[:5])9. when.py
Scenario: Common date and time operations with when.py.
import when
# Current local time
now = when.now()
print("Now (local):", now)
# UTC conversion
utc_time = when.utcnow()
print("UTC Time:", utc_time)
# Date calculation
one_month_later = when.one_month_later()
print("One Month Later:", one_month_later)
# Parse a date string
parsed_date = when.parse("2024-10-15 15:10")
print("Parsed Date:", parsed_date)10. moment
Scenario: Date/time handling inspired by Moment.js, using the moment library.
from moment import Moment
# Create current time
now = Moment.now()
print("Now (local):", now)
# Timezone conversion
utc_time = now.utc()
print("UTC Time:", utc_time)
# Date arithmetic
one_week_later = now.add(weeks=1)
print("One Week Later:", one_week_later)
# Format output
formatted_time = one_week_later.format("YYYY-MM-DD HH:mm:ss ZZ")
print("Formatted Time:", formatted_time)Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
