datefind
Find the dates in English text. datefind returns each date with the text that matched, its position in the text, and how precise the text is (a day, a week, a month, a quarter, a half year, or a year).
from datetime import date
from datefind import find_dates
text = "Invoice sent Friday, January 5, 2024. Payment is due in 2 weeks. Report covers Q2 2024."
for found in find_dates(text, tz="UTC", now=date(2024, 3, 14)):
print(found.match, found.value, found.precision)
Friday, January 5, 2024 2024-01-05 day
in 2 weeks 2024-03-28 day
Q2 2024 2024-Q2 quarter
datefind reads the year, month, and day only. It does not read times of day.
Installation
Requires Python 3.11 or higher.
pip install datefind
Supported formats
The examples use now=date(2024, 3, 14), a Thursday.
| Kind | Example text | Date found | Precision |
|---|---|---|---|
| All numeric | 2024-01-05 |
2024-01-05 | day |
01/05/2024 |
2024-01-05 | day | |
20240105 |
2024-01-05 | day | |
| Month name | January 5th, 2024 |
2024-01-05 | day |
5 Jan 2024 |
2024-01-05 | day | |
Jan 5 '24 |
2024-01-05 | day | |
twenty fifth of March |
2024-03-25 | day | |
March 5 |
2024-03-05 | day | |
| Month and year | March 2025 |
2025-03-01 | month |
Jan '24 |
2024-01-01 | month | |
12/2024 |
2024-12-01 | month | |
| Quarter | Q3 2024, 2024 Q3 |
2024-07-01 | quarter |
3rd quarter of 2024 |
2024-07-01 | quarter | |
Q3'24, 3Q24 |
2024-07-01 | quarter | |
| Half year | H2 2024, 2024 H2 |
2024-07-01 | half |
second half of 2024 |
2024-07-01 | half | |
| ISO week | 2024-W23, week 23 2024 |
2024-06-03 | week |
| Relative word | today |
2024-03-14 | day |
day after tomorrow |
2024-03-16 | day | |
next week |
2024-03-21 | week | |
last month |
2024-02-14 | month | |
next year |
2025-03-14 | year | |
| Counted offset | 3 days ago |
2024-03-11 | day |
in 2 weeks |
2024-03-28 | day | |
2 weeks after Jan 5, 2024 |
2024-01-19 | day | |
| Day of a relative month | 3rd of next month |
2024-04-03 | day |
| Weekday | Friday |
2024-03-15 | day |
next Tuesday |
2024-03-19 | day | |
last Monday |
2024-03-11 | day |
Usage
find_dates
find_dates() takes a string and returns a generator of FoundDate objects, in the order they occur in the text.
Arguments
text- The text to search for dates.first- Which part comes first in an all-number date that can be read more than one way, such as01/02/03:month(the default),day, oryear.tz- The timezone of the returned datetimes, as a name such asAmerica/New_Yorkor atzinfoobject such asZoneInfo("Europe/Paris"). Defaults to the local timezone.now- The date that relative dates such astomorrowor3 days agoresolve against, as adateor adatetime. An awaredatetimeis converted totz. A naivedatetimeis read as a time intz. Defaults to the current time.short_digit_dates- IfTrue, runs of 4 to 7 digits with no separators, such as9819or120524, match as dates. Runs of 8 digits, such as20240512, always match. Defaults toFalse, because short digit runs in text are usually years, page numbers, or quantities.
Pass now to get the same results on every run, for example in tests.
find_date
find_date() takes the same arguments as find_dates() and returns the first FoundDate in the text, or None if the text has no date.
from datefind import find_date
found = find_date("Due Jan 5, 2024 or Feb 1, 2024", tz="UTC")
print(found.value if found else "no date") # 2024-01-05
FoundDate
Each FoundDate has these fields:
datetime- The date as adatetimeat midnight intz.match- The text that matched, taken from the original text.span- The start and end positions ofmatchin the original text.precision- The unit of time that the text names:day,week,month,quarter,half, oryear.
It also has these computed properties:
date- The calendar day ofdatetime, as adate.value- The day and its precision as one standard string. Strings of the same precision sort in date order.
precision |
value format |
Example |
|---|---|---|
day |
YYYY-MM-DD |
2024-03-05 |
week |
YYYY-Www |
2024-W10 |
month |
YYYY-MM |
2024-03 |
quarter |
YYYY-Qn |
2024-Q1 |
half |
YYYY-Hn |
2024-H1 |
year |
YYYY |
2024 |
A week uses the ISO week-numbering year, so the week that starts on December 30, 2024 is 2025-W01.
as_dict() returns the found date as plain values that you can store or write as JSON, typed as FoundDateDict. date holds the ISO date with no time or timezone, and span is a list:
from datefind import find_date
found = find_date("Paid in March 2024.", tz="UTC")
print(found.as_dict())
{
"value": "2024-03",
"date": "2024-03-01",
"precision": "month",
"match": "March 2024",
"span": [8, 18],
}
A FoundDate is immutable. Use dataclasses.replace() to make a changed copy.
How dates are read
Dates that can be read more than one way
An all-number date such as 01/02/03 can put the month, the day, or the year first. The first argument decides:
first |
01/02/03 |
01/02/2024 |
|---|---|---|
month (default) |
2003-01-02 | 2024-01-02 |
day |
2003-02-01 | 2024-02-01 |
year |
2001-02-03 | no date |
A date that cannot be real in the chosen order, such as 13/01/2024 with first="month", gives no date.
Years
- Dates with a month name accept years from 1000 to 2999, such as
March 1105ordec 31 1899. - All-number dates with a day, a month, and a year accept years from 1000 to 2999 when
/,-, or.divides the parts, such as12/25/1850. - Every other all-number date must have a year from 1900 to 2099. This applies to dates divided by spaces, a month and year without a day such as
12/2024, and runs of digits. This rule keeps counts, prices, and ranges such as10 2500 sold,1500.05, orpages 10-1500from matching as dates. - A two-digit year takes the century that puts it within 50 years of
now. Withnowin 2026,75becomes 2075 and76becomes 1976. An apostrophe can mark a two-digit year, as inJan 5 '24,Jan '24, orQ3 '24.
Missing parts
- A date without a year takes the year of
now.March 5withnowin 2024 gives March 5, 2024. - A month, quarter, half year, or year without a day gives the first day of that period.
March 2025gives March 1, 2025, andQ3 2024gives July 1, 2024. - An ISO week gives the Monday of that week.
2024-W23gives June 3, 2024. A week that does not exist in its year, such as2021-W53, gives no date.
Relative dates
last,this, andnextwithweek,month, oryeargive the day one period fromnow.next monthon March 14 gives April 14. The precision is the period, so read the result as "some day in that month".- A counted offset such as
3 days agoorin 2 weeksgives one day, so its precision isday. - A counted offset before a date shifts that date:
2 weeks after Jan 5, 2024gives January 19, 2024. If the shift lands on a day that does not exist, as in1 month after Jan 31, 2024, there is no date. - A weekday alone, or with
next, gives the next one afternow, nevernowitself.lastgives the previous one.thisgives the next one within 0 to 6 days, so it can benow.
What the match includes
A match holds only the words that change the date:
next,last, andthis, as innext Friday.- A counted offset and its direction word, as in
2 weeks after Jan 5, 2024. - A weekday directly before a date, as in
Friday, January 5, 2024. The two give one date, not a date and a separate weekday. If the date states its year and the weekday does not agree with it, as inTuesday, January 5, 2024, the weekday is not part of the match.
Words that only introduce a date, such as on, at, by, due, or the, are never part of the match. In due on the 3rd of next month, the match is 3rd of next month.
Text that is not a date
These rules keep common text from matching:
- Because
mayis also a verb, a plain number before it is not a date (option 2 may work). Write2nd Mayor2 of Mayinstead. - The weekday abbreviations
sat,sun, andwedare also English words. Alone, they match only afterlast,this, ornext. Before a date, they need a comma or a period, as inWed, 03 Jan 2024. - Month abbreviations such as
marormaymatch only as separate words, soMarketandMayfieldare not months. Full month names can touch other letters. - A version number after
vorversion, such asv2.1.1500, is not a date. - A day range in one month, such as
Jan 5-10, 2024orJan 5 to 10, gives no date. Neither end of the range is reported, so the10is not read as the year 2010. - A date that does not exist, such as
February 30, 2024, gives no date.
Times and special characters
- A time after a date, as in
2024-01-05 14:30,Jan 5 10pm, or the file nameIMG_20240512_143012.jpg, is ignored, and the date before it is found. - Text copied from web pages and word processors works without cleanup. Non-breaking spaces, en dashes, curly apostrophes, and full-width digits match the same as their plain forms.
matchandspanrefer to the original text.
Comparison with other libraries
- dateparser reads more than 200 languages and times of day. Its
search_dates()returns pairs of text and datetime without positions in the text, and returnsNonewhen it finds nothing. Use it for languages other than English or for times. - datefinder finds dates and times in English text and returns datetimes. Its
source=Trueandindex=Trueoptions change each result into a tuple that holds the matched text and positions. datefindreads English dates only. Every result holds the matched text, its position, and its precision. It does not fill a missing day with today's day, and it gives no date for a date that does not exist, instead of a nearby one.
Contributing
See CONTRIBUTING.md for more information.
Metadata
Release files for datefind 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| datefind-1.0.1.tar.gz | 27.3 kB | Without provenance | Details for datefind-1.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| datefind-1.0.1-py3-none-any.whl | Python 3 | none | any | Details for datefind-1.0.1-py3-none-any.whl |
Total release size: 55.2 kB
Release files / datefind-1.0.1.tar.gz
| Download URL | datefind-1.0.1.tar.gz |
|---|---|
| Size | 27.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a10c9b4d4b605f7329a1c888f96eaf5a02d142afa12b41472f4c86b04242bd91
|
|
BLAKE2b-256 checksum How to use checksums |
61f4934f203fa92cf0fa7519cbebe4eb4fdc2365eb7a6a3661832f08804eae84
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / datefind-1.0.1-py3-none-any.whl
| Download URL | datefind-1.0.1-py3-none-any.whl |
|---|---|
| Size | 27.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
39e7bbbd64d6f8d8b6735914a394e6f4be256f3f9c35d796d0ade23c52baf540
|
|
BLAKE2b-256 checksum How to use checksums |
c2a65c7bf1a111a9a31a86216fd4d883c6065fca3e800a3ac35f2e9a2873c31b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for datefind 1.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.