Extensions to the standard Python datetime module
—
Flexible parsing of date and time strings in almost any format, with support for ambiguous date resolution, timezone parsing, fuzzy parsing, and high-performance ISO-8601 parsing.
The primary entry point for parsing date/time strings with extensive configuration options.
def parse(timestr, parserinfo=None, **kwargs):
"""
Parse a date/time string into a datetime object.
Parameters:
- timestr (str): String containing date and/or time
- parserinfo (parserinfo, optional): Parser configuration object
- default (datetime, optional): Default datetime for missing components
- ignoretz (bool): If True, ignore timezone info and return naive datetime
- tzinfos (dict/callable, optional): Additional timezone name mappings
- dayfirst (bool): Interpret ambiguous dates as day-first (DD/MM/YYYY)
- yearfirst (bool): Interpret ambiguous dates as year-first (YYYY/MM/DD)
- fuzzy (bool): Ignore unknown tokens in the string
- fuzzy_with_tokens (bool): Return (datetime, tokens) tuple with unknown tokens
Returns:
datetime: Parsed datetime object
Raises:
ParserError: When string cannot be parsed
"""
class ParserError(ValueError):
"""Raised when a date/time string cannot be parsed."""Usage Examples:
from dateutil.parser import parse
from datetime import datetime
# Basic parsing - various formats
dt1 = parse("2023-12-25") # ISO format
dt2 = parse("Dec 25, 2023") # Named month
dt3 = parse("25/12/2023") # Ambiguous format
dt4 = parse("2023-12-25T14:30:00Z") # ISO with timezone
# Ambiguous date resolution
parse("12/25/2023") # Assumes MM/DD/YYYY (US)
parse("12/25/2023", dayfirst=True) # Forces DD/MM/YYYY (EU)
parse("2023/12/25", yearfirst=True) # YYYY/MM/DD format
# Default values for missing components
default = datetime(2023, 1, 1, 9, 0, 0)
parse("Dec 25", default=default) # Uses year/time from default
# Timezone handling
parse("2023-12-25 14:30 EST") # With timezone name
parse("2023-12-25 14:30 +0500") # With offset
parse("2023-12-25 14:30 EST", ignoretz=True) # Ignore timezone
# Fuzzy parsing
parse("Today is Dec 25, 2023", fuzzy=True) # Extracts date from text
dt, tokens = parse("Event on Dec 25, 2023 at venue", fuzzy_with_tokens=True)High-performance parser specifically optimized for ISO-8601 formatted strings.
def isoparse(dt_str):
"""
Parse ISO-8601 formatted date/time strings.
Parameters:
- dt_str (str): ISO-8601 formatted string
Returns:
datetime: Parsed datetime object
Raises:
ValueError: When string is not valid ISO-8601 format
"""
class isoparser:
def __init__(self, sep=None):
"""
ISO-8601 parser with configurable date/time separator.
Parameters:
- sep (str, optional): Custom separator between date and time parts
"""
def isoparse(self, dt_str):
"""Parse ISO-8601 string using this parser instance."""Usage Examples:
from dateutil.parser import isoparse, isoparser
# Standard ISO-8601 formats
dt1 = isoparse("2023-12-25") # Date only
dt2 = isoparse("2023-12-25T14:30:00") # Date and time
dt3 = isoparse("2023-12-25T14:30:00Z") # With UTC timezone
dt4 = isoparse("2023-12-25T14:30:00+05:00") # With offset timezone
dt5 = isoparse("2023-12-25 14:30:00") # Space separator
# Custom parser with different separator
parser = isoparser(sep='_')
dt = parser.isoparse("2023-12-25_14:30:00")Pre-configured parser instances available for direct use.
DEFAULTPARSER: parser
"""Default parser instance used by module-level parse() function."""
DEFAULTTZPARSER: parser
"""Default timezone-aware parser instance for internal use."""Configurable parser class for customizing parsing behavior and locale settings.
class parser:
def __init__(self, parserinfo=None):
"""
Create a parser with custom configuration.
Parameters:
- parserinfo (parserinfo, optional): Parser configuration object
"""
def parse(self, timestr, default=None, ignoretz=False, tzinfos=None, **kwargs):
"""
Parse date/time string using this parser's configuration.
Parameters: Same as module-level parse() function
Returns: datetime object
"""
class parserinfo:
def __init__(self, dayfirst=False, yearfirst=False):
"""
Parser configuration for locale-specific parsing.
Parameters:
- dayfirst (bool): Default day-first interpretation
- yearfirst (bool): Default year-first interpretation
"""
# Configuration attributes
JUMP: list[str] # Words to ignore ("at", "on", "the")
WEEKDAYS: list[list[str]] # Weekday names by language
MONTHS: list[list[str]] # Month names by language
HMS: list[list[str]] # Hour/minute/second indicators
AMPM: list[str] # AM/PM indicators
UTCZONE: list[str] # UTC timezone names
PERTAIN: list[str] # Pertaining words ("of", "in")Usage Examples:
from dateutil.parser import parser, parserinfo
# Custom parser info for different locale
class CustomParserInfo(parserinfo):
MONTHS = [
["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
["Januar", "Februar", "März", "April", "Mai", "Juni",
"Juli", "August", "September", "Oktober", "November", "Dezember"]
]
custom_parser = parser(CustomParserInfo())
dt = custom_parser.parse("25 März 2023") # German month name
# Parser with default settings
eu_parser = parser(parserinfo(dayfirst=True))
us_parser = parser(parserinfo(dayfirst=False))
dt_eu = eu_parser.parse("12/03/2023") # March 12, 2023
dt_us = us_parser.parse("12/03/2023") # December 3, 2023Support for custom timezone mappings and resolution.
class UnknownTimezoneWarning(RuntimeWarning):
"""Warning issued when encountering unknown timezone names."""
# Timezone resolution in parse() function
def parse(timestr, tzinfos=None, **kwargs):
"""
tzinfos parameter options:
- dict: Mapping of timezone names to tzinfo objects
- callable: Function that takes timezone name and returns tzinfo object
"""Usage Examples:
from dateutil.parser import parse
from dateutil.tz import gettz
# Custom timezone mappings
custom_timezones = {
'EST': gettz('America/New_York'),
'PST': gettz('America/Los_Angeles'),
'JST': gettz('Asia/Tokyo')
}
dt = parse("2023-12-25 14:30 JST", tzinfos=custom_timezones)
# Dynamic timezone resolution
def resolve_timezone(tzname):
"""Custom timezone resolver function."""
timezone_map = {
'ET': 'America/New_York',
'PT': 'America/Los_Angeles',
'MT': 'America/Denver'
}
if tzname in timezone_map:
return gettz(timezone_map[tzname])
return None
dt = parse("2023-12-25 14:30 ET", tzinfos=resolve_timezone)from dateutil.parser import parse
text = "Meeting scheduled for December 25th, 2023 at the main office"
dt, tokens = parse(text, fuzzy_with_tokens=True)
print(f"Date: {dt}")
print(f"Ignored tokens: {tokens}") # ['Meeting', 'scheduled', 'for', 'at', 'the', 'main', 'office']from dateutil.parser import parse, ParserError
date_strings = [
"2023-12-25",
"Dec 25, 2023",
"25/12/2023",
"invalid date"
]
parsed_dates = []
for date_str in date_strings:
try:
dt = parse(date_str, dayfirst=True)
parsed_dates.append(dt)
except ParserError:
print(f"Could not parse: {date_str}")from dateutil.parser import parse, isoparse
# For ISO-8601 strings, isoparse is much faster
iso_string = "2023-12-25T14:30:00Z"
dt_fast = isoparse(iso_string) # Faster
dt_slow = parse(iso_string) # Slower but more flexible
# For non-ISO strings, use parse
natural_string = "Dec 25, 2023 2:30 PM"
dt = parse(natural_string) # Only option# Standard datetime types (from datetime module)
from datetime import datetime, tzinfo
# Parser-specific exceptions
class ParserError(ValueError):
"""Exception raised when parsing fails."""
class UnknownTimezoneWarning(RuntimeWarning):
"""Warning for unknown timezone names."""
# Parser configuration types
class parserinfo:
JUMP: list[str]
WEEKDAYS: list[list[str]]
MONTHS: list[list[str]]
HMS: list[list[str]]
AMPM: list[str]
UTCZONE: list[str]
PERTAIN: list[str]
# Timezone info parameter types
TzInfos = dict[str, tzinfo] | callable[[str], tzinfo | None] | NoneInstall with Tessl CLI
npx tessl i tessl/pypi-python-dateutil