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.3.tar.gz (5.3 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.3-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: human_date_parser-0.1.3.tar.gz
  • Upload date:
  • Size: 5.3 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.3.tar.gz
Algorithm Hash digest
SHA256 a5ed3d86d2e676af24e71eeb6343c3b99a78d835132b36d5a79402616d6554e5
MD5 b8e9fd306e761aa28493bc51b37b5ab8
BLAKE2b-256 ee8ad1e8dbc678609a4fff763e82d1880e1bf8f528b32ddfec93617b63211226

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for human_date_parser-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2e4772e74d5c99827d3c3c983938c266132744247ead236b33918d90ae727143
MD5 8c9a522e7db4d6400f66f0cbb5cade90
BLAKE2b-256 aa79791e7b7e58048a277299305e4a9209a28a42549685e6ab7813a44dc02b29

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