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 parsedatetimeVerified import paths — ran on the pinned version, not inferred.
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).
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])`.
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.
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))`.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.
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)
```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)
```Install the package using pip in your terminal: ```bash pip install parsedatetime ```
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)
```