parsedatetime

2.6 · active · verified Sun Mar 29

parsedatetime is a Python module that can parse human-readable date/time strings like "tomorrow at 3pm" or "next Tuesday". It is currently at version 2.6 and primarily targets Python 3, with v2.6 maintaining Python 2.7 compatibility. Releases occur periodically, with significant updates and bug fixes.

Warnings

Install

Imports

Quickstart

Initializes the Calendar object and demonstrates parsing a human-readable date/time string, converting the `time.struct_time` output to a standard `datetime` object, and using a `sourceTime` for relative parsing. The `parse()` method returns a tuple: `(time_struct, parse_status)`, where `parse_status` indicates success and the type of information parsed (e.g., date, time, or datetime).

from datetime import datetime
import parsedatetime

cal = parsedatetime.Calendar()

# Parse a human-readable string
time_struct, parse_status = cal.parse("tomorrow at 3pm")

# Convert the result to a Python datetime object
if parse_status != 0:
    dt_object = datetime(*time_struct[:6])
    print(f"Parsed 'tomorrow at 3pm' as: {dt_object}")

# Example with a specific starting point (sourceTime)
from datetime import datetime, timedelta
source_time = datetime(2026, 1, 1, 10, 0, 0) # Jan 1, 2026, 10:00 AM
time_struct_next, _ = cal.parse("next friday", source_time)
dt_object_next = datetime(*time_struct_next[:6])
print(f"Parsed 'next friday' from {source_time} as: {dt_object_next}")

view raw JSON →