parse()
DateParser.parse() is the method most applications should call.
parser.parse(text) -> datetime.datetime | None
Signature
parse(
text,
noprefix=False,
nocharsetfilter=False,
noseparatorfilter=False,
noyearformatfilter=False,
nolanguagefilter=False,
allow_no_year=True,
*,
languages=None,
date_order=None,
reference=None,
prefer=None,
)
Pass the options by keyword: parse() and match() order their positional
parameters differently.
| Argument | Default | Meaning |
|---|---|---|
text | required | Input string |
allow_no_year | True | Include day-month patterns without a year; the current year is filled in. noyear= is a deprecated alias |
noprefix | False | Skip prefix-based candidate filtering |
nocharsetfilter | False | Skip character-set filtering |
noseparatorfilter | False | Skip separator filtering |
noyearformatfilter | False | Skip 2-digit vs 4-digit year filtering |
nolanguagefilter | False | Skip automatic language narrowing |
languages | None | Languages to try first; see per-call options |
date_order | None | "DMY", "MDY" or "YMD" for an ambiguous numeric date; None uses the parser's |
reference | None | "Now" for a date without a year: its year is used. Default: the current time |
prefer | None | "past" or "future": move a date without a year across the year boundary relative to reference |
Leave the no* flags at False unless you are debugging a miss. Each flag
skips one level of the filter pipeline. See also
troubleshooting.
Return value
- A
datetime.datetimewhen a supported pattern matches at index 0 and the components form a valid calendar date. It is naive unless the parser was built withtz_aware=Trueand the input carries a zone after the time (+0300,+03:00,Z,GMT+3,MSK). Nonewhen nothing matches, the string is empty/whitespace, ordatetime.datetime(**components)would raiseValueError.
parse() never raises for ordinary bad input.
Behavior notes
- Matching is left-aligned. Trailing text after the date is ignored.
- A time of day populates hour, minute, and second:
HH:MM,HH:MM:SS,14h30/14 h 30, or the 12-hour clock (3:45 PM,11:05:09 a.m.,3 p.m.;12:00 AMis midnight). It can follow the date (after,,|or the ISOT) or come first (12:30, 5 мая 2022,14:30 | 5 May 2022,12:30 05.05.2022). - A word the date's language writes before the time is accepted:
at(en),à(fr),um(de),a las(es),alle(it),om(nl),às(pt),в(ru):4 Сентября 2020 в 04:06→datetime(2020, 9, 4, 4, 6). Numeric dates accept any of them (05.05.2022 в 12:30). - Chinese, Japanese and Korean numeric dates:
2022年5月5日,2022年5月5日,2022년 5월 5일, with a weekday ((木),星期四,(목)) and a time (14:30,14時30分,午後2時30分,下午2:30,오후 2:30). Without a year (5月5日,5월 5일) they arenoyearpatterns. They belong to no language. - Patterns flagged
noyear(05.12,4 Сентября,May 5,5th May,Sept. 5) substitute the year ofreference(the current year by default) whenallow_no_year=True(the default);prefer="past"/"future"moves them across the year boundary. Withallow_no_year=Falsethey do not match. - Two-digit years pass through as-is (
99→ year 99) unless the parser was built withpivot_year=(for exampleDateParser(pivot_year=1968)maps99→ 1999,20→ 2020).pivot_yearmust be an integer;pivot_year=TrueraisesTypeError. - A zone after the time (
+0300,+03:00,Z,GMT+3,UTC-05:30,MSK/МСК,CEST,BST,PDT, …) is attached only withDateParser(tz_aware=True). Latin abbreviations are matched in capitals only, so the Frenchestafter a time is not US Eastern time. - Relative phrases (
today,3 days ago) returnNone; useparse_relative().
Examples
import datetime
import qddate
parser = qddate.DateParser()
assert parser.parse("01.12.2009") == datetime.datetime(2009, 12, 1)
assert parser.parse("12.03.1999 Hello people") == datetime.datetime(1999, 3, 12)
assert parser.parse("16 May 2009 14:10") == datetime.datetime(2009, 5, 16, 14, 10)
assert parser.parse("totally invalid date") is None