parsedatetime
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
- breaking The `parse()` method's return value changed significantly around version 2.0. It now consistently returns a tuple `(time_struct, parse_status)`. Code relying on direct `datetime` or `time_struct` return without checking `parse_status` or handling the tuple will break.
- deprecated The 'flag style' for instantiating `Calendar()` (e.g., `Calendar(parsedatetime.constants.getConstants())`) was deprecated in version 2.0 in favor of a 'context style'.
- gotcha When parsing incomplete human-readable dates (e.g., "Jan 1st"), `parsedatetime` implicitly guesses the year based on the current date (`sourceTime`). This can lead to unexpected results if the inferred year doesn't match expectations, especially for dates far in the past or future.
- gotcha While v2.6 includes Python 2.7 compatibility, the library's development now primarily targets Python 3. Users on older Python 2.x environments may encounter unexpected issues or lack of support in future releases.
Install
-
pip install parsedatetime
Imports
- Calendar
import parsedatetime cal = parsedatetime.Calendar()
Quickstart
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}")