Registry / data / parsedatetime

parsedatetime

JSON →
library2.6pypypi✓ verified 31d ago

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.

pip install parsedatetime
INSTALL
IMPORT
SIG · PARSEDATETIME
P
parsedatetime
datapythonv2.6
Install
1.6s avg
Import
60ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v2.6 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.066s · 18.1MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 1.6s · import 0.054s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Calendar
✓ import parsedatetime cal = parsedatetime.Calendar()
✗ import parsedatetime.parsedatetime as pdt cal = pdt.Calendar()
The direct import from `parsedatetime` is the current standard. Older examples or codebases might use `parsedatetime.parsedatetime`.

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}")
Debug
Known issues
breakingThe `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.
fix
Always expect a `(time_struct, parse_status)` tuple from `cal.parse()`. Check `parse_status` (0 for failure, 1 for date, 2 for time, 3 for datetime) before converting `time_struct` to a `datetime` object, e.g., `datetime(*time_struct[:6])`.
affects: >=2.0
deprecatedThe 'flag style' for instantiating `Calendar()` (e.g., `Calendar(parsedatetime.constants.getConstants())`) was deprecated in version 2.0 in favor of a 'context style'.
fix
Instantiate `Calendar()` directly without arguments for default behavior, or use `Calendar(version=parsedatetime.VERSION_CONTEXT_STYLE)` for explicit context-aware parsing if needed, though this is often the default behavior in newer versions.
affects: >=2.0
gotchaWhen 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.
fix
For critical date parsing, always provide a `sourceTime` argument to `cal.parse()` to set a clear reference point, or explicitly include the year in the input string. Example: `cal.parse("Jan 1st", sourceTime=datetime(2025, 6, 1))`.
affects: All
gotchaWhile 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.
fix
Prefer using `parsedatetime` in a Python 3 environment. If Python 2.7 is necessary, ensure you are on `parsedatetime` version 2.6 and thoroughly test your parsing logic. Upgrade to Python 3 where possible.
affects: Potentially problematic on Python 2.x, especially <2.7
Errors
Common errors & fixes
AttributeError: module 'parsedatetime' has no attribute 'parseDT'
The `parseDT` method is an instance method of the `Calendar` class and must be called on an instantiated `Calendar` object, not directly on the `parsedatetime` module.
fix
First, create an instance of `parsedatetime.Calendar()` and then call its `parseDT` method:
```python
import parsedatetime as pdt
import datetime

cal = pdt.Calendar()
result, parse_status = cal.parseDT('tomorrow', datetime.datetime.now())
print(result)
```
AttributeError: 'tuple' object has no attribute 'year'
The `parseDT` method returns a tuple `(datetime_object, parse_status)`, but the user attempted to access datetime attributes (like 'year', 'month', 'day') directly on this tuple instead of on the datetime object within it.
fix
Unpack the tuple into separate variables for the datetime object and the parse status, or access the datetime object using its index `[0]`:
```python
import parsedatetime as pdt
import datetime

cal = pdt.Calendar()

# Option 1: Unpack the tuple
dt_obj, parse_status = cal.parseDT('next Tuesday', datetime.datetime.now())
print(dt_obj.year, dt_obj.month, dt_obj.day)

# Option 2: Access by index
dt_obj = cal.parseDT('next Tuesday', datetime.datetime.now())[0]
print(dt_obj.year, dt_obj.month, dt_obj.day)
```
ModuleNotFoundError: No module named 'parsedatetime'
The `parsedatetime` package is not installed in the Python environment being used, or the environment's `PYTHONPATH` does not include the installation location.
fix
Install the package using pip in your terminal:
```bash
pip install parsedatetime
```
TypeError: unsupported operand type(s) for -: 'NoneType' and 'datetime.timedelta'
This error typically occurs when `None` is passed as the `sourceTime` (reference date/time) argument to `parseDT`, but the method expects a valid `datetime.datetime` object to calculate relative dates.
fix
Ensure that a valid `datetime.datetime` object (e.g., `datetime.datetime.now()`) is always provided for the `sourceTime` argument when calling `parseDT`:
```python
import parsedatetime as pdt
import datetime

cal = pdt.Calendar()

# Correct: Pass a datetime object as sourceTime
dt_obj, parse_status = cal.parseDT('tomorrow', datetime.datetime.now())
print(dt_obj)

# Incorrect (would cause the error):
# dt_obj, parse_status = cal.parseDT('tomorrow', None)
```
Upgrade
Version history
2.6latest on PyPI · released May 31, 2020
Audit
Dependencies
pyicuoptionalUsed for advanced locale-aware parsing and robust testing, but is an optional runtime dependency for basic functionality.
pytzoptionalRequired for timezone-aware operations, as demonstrated in quickstart examples.
Agent activity
11 hits · last 30 days
node
11
Resources
parsedatetime — pip install parsedatetime · libregistry