Convert natural language date strings into Python datetime objects.
Project description
human-date-parser
A lightweight and intuitive Python library designed to effortlessly convert natural language date expressions into precise Python datetime objects. Simplify date parsing in your applications by understanding human-friendly inputs like "today," "next Monday," or "in 3 days."
Features
- Effortless Parsing: Convert common natural language date phrases into datetime objects.
- Relative Date Handling: Accurately interpret "today," "tomorrow," "yesterday," and relative terms like "in X days/weeks/months/years" or "X days/weeks/months/years ago."
- Weekday Recognition: Understand "next Monday," "last Friday," and similar weekday references.
- Complex Expressions: Handle combinations like "in 2 weeks and 3 days."
- Robust Error Handling: Returns None for unparsable input, allowing for graceful error management.
Installation
You can install human-date-parser directly from your terminal:
pip install human-date-parser
If you are developing the library locally, you can install it in editable mode:
pip install -e .
Usage
The primary function of the library is parse(), which takes a string representing a natural language date and returns a datetime.datetime object. All parsed dates will have their time components set to 00:00:00 (midnight) unless specified otherwise in the natural language input (though this library primarily focuses on date parsing, not time).
Code Examples
Let's explore how to use parse() with various natural language inputs.
from human_date_parser import parse
import datetime
# For consistent examples, we'll assume the current date is July 12, 2025.
# In a real application, parse_date will use the actual current date.
📅 Basic Dates
Easily parse common, absolute date references.
print(parse("today")) # -> datetime.datetime(2025, 7, 12, 0, 0)
print(parse("tomorrow")) # -> datetime.datetime(2025, 7, 13, 0, 0)
print(parse("yesterday")) # -> datetime.datetime(2025, 7, 11, 0, 0)
⏳ Relative Future
Calculate dates relative to the current day, looking forward.
print(parse("in 3 days")) # -> datetime.datetime(2025, 7, 15, 0, 0)
print(parse("in 1 week")) # -> datetime.datetime(2025, 7, 19, 0, 0)
print(parse("in 2 months")) # -> datetime.datetime(2025, 9, 12, 0, 0)
⌛ Relative Past
Calculate dates relative to the current day, looking backward.
print(parse("2 days ago")) # -> datetime.datetime(2025, 7, 10, 0, 0)
print(parse("3 weeks ago")) # -> datetime.datetime(2025, 6, 21, 0, 0)
print(parse("1 year ago")) # -> datetime.datetime(2024, 7, 12, 0, 0)
🗓️ Weekdays
Determine the date of the next or last occurrence of a specific weekday.
print(parse("next Monday")) # → Next upcoming Monday (e.g., datetime.datetime(2025, 7, 14, 0, 0))
print(parse("last Friday")) # → Previous Friday (e.g., datetime.datetime(2025, 7, 11, 0, 0))
🧠 Complex Natural Language
The parser can combine multiple relative terms for more nuanced date calculations.
print(parse("in 2 weeks and 3 days")) # → Adds both weeks and days (e.g., datetime.datetime(2025, 7, 29, 0, 0))
print(parse("next year")) # → Approx. one year ahead (e.g., datetime.datetime(2026, 7, 12, 0, 0))
print(parse("last month")) # → One month before today (e.g., datetime.datetime(2025, 6, 12, 0, 0))
⚠️ Invalid Input Handling
For inputs that cannot be parsed into a valid date, the function gracefully returns None.
result = parse("not a date")
if result is None:
print("Could not parse date.")
# Output: Could not parse date.
🧪 Example in Real Use
Here's a practical example of how you might use human-date-parser to set a reminder.
# Schedule a reminder 5 days from now
reminder_date = parse("in 5 days")
if reminder_date:
print("Reminder set for:", reminder_date.strftime("%Y-%m-%d"))
# Output (assuming today is July 12, 2025): Reminder set for: 2025-07-17
else:
print("Failed to set reminder. Please check the date input.")
Contributing
Contributions are welcome! If you have suggestions for new features, improvements, or bug fixes, please open an issue or submit a pull request on the project's GitHub repository.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file human_date_parser-0.1.4.tar.gz.
File metadata
- Download URL: human_date_parser-0.1.4.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe620dc0c3f71fce633caf2fb8a47be68661034ba4069d73363ea98216efb2f1
|
|
| MD5 |
ee349a319380fe31ab9f7455b579486b
|
|
| BLAKE2b-256 |
336732f6ce745ff149533a75e2384acf2e768ae7cc4923bb72329723a4347d1f
|
File details
Details for the file human_date_parser-0.1.4-py3-none-any.whl.
File metadata
- Download URL: human_date_parser-0.1.4-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c9f2b97bbee0665ff7952ebe3d724051dc2dd0580f403eba4925a5b5df1b937d
|
|
| MD5 |
9fcaa93b0d448c8a5941d25a392ad0b7
|
|
| BLAKE2b-256 |
503c1641738c54869ced7041da2ba904ef7cc34241b07ecc7a67f29fcb03aa5e
|