Skip to main content

A python library for parsing natural language time descriptions.

Installation

Dateparse is on PyPi; install with Pip: $ pip install dateparse

Usage

>>> import dateparse
>>> from datetime import date
>>> # The main use case is extracting a single date from a string
>>> dateparse.basic_parse(date.today(), "a week from friday")
DateResult(date=datetime.date(2023, 2, 10), start=0, end=15, content='a week from fri')
>>> # by default the first (leftmost) encountered date is returned
>>> dateparse.basic_parse(date.today(), "a week from thursday and a week from friday")
DateResult(date=datetime.date(2023, 2, 9), start=0, end=15, content='a week from thu')
>>> # the from_right option changes this
>>> dateparse.get_first(date.today(), "a week from thursday and a week from friday")
DateResult(date=datetime.date(2023, 2, 10), start=0, end=15, content='a week from fri')
>>> # default behavior for all parse functions is to get the next future date matching the expression
>>> # relative to the given base date
>>> # this can be changed with the allow_past option
>>> dateparse.basic_parse(date(1970, 9, 8), "january 1", allow_past=True)
DateResult(date=datetime.date(1970, 1, 1), start=0, end=9, content=' january 1')
>>> # parse_all gets all expressions in a list
>>> dateparse.parse_all(date.today(), "a week from thursday and four days before march 11")
[DateResult(date=datetime.date(2023, 2, 9), start=0, end=15, content='a week from thu'), DateResult(date=datetime.date(2023, 3, 7), start=24, end=50, content='four days before march 11')]
>>> # the default return type for dates is a DateResult, a simple named tuple containing the date's info
>>> # For convenience, there are also functions to just get the date
>>> dateparse.basic_date_parse(date.today(), "february 9")
datetime.date(2023, 2, 9)
>>> # parse_all_dates works in the same way
>>> # a DateParser object holds a specified baseline date
>>> # by default, assumes the baseline date is date.today()
>>> parser = dateparse.DateParser()
>>> # parses dates with a reference point of january 17, 2021
>>> parser_january = dateparse.DateParser(base_date = date(2021, 17, 1))
>>> # DateParser also supports named days by default
>>> parser.get_first("four days after halloween 2024")
DateResult(date=datetime.date(2024, 11, 4), start=0, end=31, content='four days after october 31 2024')
>>> # You can also define your own custom named days as a string dictionary and pass it into the parser
>>> my_dates = {'my birthday' : 'june 11'}
>>> my_parser = dateparse.DateParser(named_days = my_dates)
>>> my_parser.get_first("a month before my birthday")
DateResult(date=datetime.date(2023, 5, 14), start=0, end=22, content='a month before june 11')
>>> # DateParser.get_first and DateParser.get_last are convenience wrappers around basic_parse
>>> # to get the first or last expression, with the base date defined at initialization
>>> my_parser.get_first("a week from thurs and two months after friday")
DateResult(date=datetime.date(2023, 2, 9), start=0, end=15, content='a week from thu')
>>> my_parser.get_last("a week from thurs and two months after friday")
DateResult(date=datetime.date(2023, 4, 3), start=21, end=42, content='two months after fri')
>>> # DateParser.get_all and DateParser.get_all_dates wrap parse_all and parse_all_dates
>>> my_parser.get_all("a week from thurs and two months after friday")
[DateResult(date=datetime.date(2023, 2, 9), start=0, end=15, content='a week from thu'), DateResult(date=datetime.date(2023, 4, 3), start=21, end=42, content='two months after fri')]
>>> my_parser.get_all_dates("a week from thurs and two months after friday")
[datetime.date(2023, 2, 9), datetime.date(2023, 4, 3)]

Other Info

This project is under active development. The core API is unlikely to change much at this point, but the under-the-hood details are still very much in flux.

Dateparse requires Python 3.10 or higher, thanks the author’s neurotic devotion to type annotations.

Release files for dateparse 1.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dateparse 1.4.0
File Size Uploaded
dateparse-1.4.0.tar.gz 11.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dateparse 1.4.0
File Interpreter ABI Platform
dateparse-1.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 23.0 kB

Release files / dateparse-1.4.0.tar.gz

Download URL dateparse-1.4.0.tar.gz
Size 11.5 kB
Tags Source
SHA-256 checksum
How to use checksums
64e4b1958b38c848f4cbae94056fb27554ae2d1c26016125f5a4105ae0c3cded
BLAKE2b-256 checksum
How to use checksums
9c24990bfca3d1958df2394087b9d899da919919c94bfeee35abd881b530402d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.10.10 Linux/6.2.10-arch1-1

Release files / dateparse-1.4.0-py3-none-any.whl

Download URL dateparse-1.4.0-py3-none-any.whl
Size 11.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1728ab1b1eba609d8213b1ff827d9984116b9a02e28add9feca9dc4fb00675fa
BLAKE2b-256 checksum
How to use checksums
360d7823f56f5fcc0e6196771858735fb2d95004dcca989e7f5dcfd58abe1e27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.10.10 Linux/6.2.10-arch1-1

Release history Release notifications | RSS feed

This release

1.4.0 This release

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.11.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page