Skip to main content

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_date 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

human_date_parser-0.1.2.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

human_date_parser-0.1.2-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file human_date_parser-0.1.2.tar.gz.

File metadata

  • Download URL: human_date_parser-0.1.2.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.5

File hashes

Hashes for human_date_parser-0.1.2.tar.gz
Algorithm Hash digest
SHA256 0cc098ab8ec3cc0742a73289a5ad1ed9ad87fc9ec924bbb419e23f22bf169ec8
MD5 b6665ae3ec7dfeb4ee975b24ff4b7950
BLAKE2b-256 0d34c12cc2ad8dff181ea3dedbf7a9be7b6b9e01c66342a54d1e40e442bfc43b

See more details on using hashes here.

File details

Details for the file human_date_parser-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for human_date_parser-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2625064c22d3effda1b09ecfc53c5d8a37f736d2c9900b8fae9d9aae480fff38
MD5 2c9fcedc754576d6e6213082681e71c2
BLAKE2b-256 375d2beaa4948e32db9e8b9f54aeba13be89a0eb6c36f203a1127567ffc8897b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page