Extensions to the standard Python datetime module
npx @tessl/cli install tessl/pypi-python-dateutil@2.9.0Powerful extensions to the standard Python datetime module, providing comprehensive functionality for date and time manipulation including relative deltas, date parsing, recurrence rules, timezone handling, and advanced date computations.
pip install python-dateutilimport dateutilIndividual module imports:
from dateutil import relativedelta, parser, rrule, tz, easter, utils, zoneinfofrom dateutil.relativedelta import relativedelta
from dateutil.parser import parse
from dateutil.tz import gettz
from datetime import datetime
# Relative date calculations
now = datetime.now()
next_month = now + relativedelta(months=1)
next_friday = now + relativedelta(weekday=4) # Friday
# Flexible date parsing
date1 = parse("2023-12-25")
date2 = parse("Dec 25, 2023")
date3 = parse("25/12/2023", dayfirst=True)
# Timezone handling
utc = gettz('UTC')
eastern = gettz('America/New_York')
dt_utc = datetime.now(utc)
dt_eastern = dt_utc.astimezone(eastern)python-dateutil is organized into specialized modules that extend datetime functionality:
Advanced date arithmetic supporting relative operations (add/subtract time periods) and absolute operations (set specific date components), with intelligent handling of complex scenarios like month-end dates and weekday calculations.
class relativedelta:
def __init__(self, dt1=None, dt2=None, years=0, months=0, days=0,
leapdays=0, weeks=0, hours=0, minutes=0, seconds=0,
microseconds=0, year=None, month=None, day=None,
weekday=None, yearday=None, nlyearday=None, hour=None,
minute=None, second=None, microsecond=None): ...
# Weekday constants
MO, TU, WE, TH, FR, SA, SU = ...Flexible parsing of date and time strings in almost any format, with support for ambiguous date resolution, timezone parsing, and high-performance ISO-8601 parsing.
def parse(timestr, parserinfo=None, **kwargs): ...
def isoparse(dt_str): ...
class parser:
def __init__(self, parserinfo=None): ...
def parse(self, timestr, default=None, ignoretz=False, tzinfos=None, **kwargs): ...
class parserinfo:
def __init__(self, dayfirst=False, yearfirst=False): ...Complete implementation of iCalendar recurrence rules (RFC 5545) for generating recurring dates, with support for complex patterns, exclusions, and efficient iteration over large date ranges.
class rrule:
def __init__(self, freq, dtstart=None, interval=1, wkst=None, count=None,
until=None, bysetpos=None, bymonth=None, bymonthday=None,
byyearday=None, byweekno=None, byweekday=None, byhour=None,
byminute=None, bysecond=None, cache=False): ...
class rruleset:
def __init__(self, cache=False): ...
def rrulestr(s, **kwargs): ...
# Frequency constants
YEARLY, MONTHLY, WEEKLY, DAILY, HOURLY, MINUTELY, SECONDLY = ...Comprehensive timezone handling supporting UTC, fixed offsets, system local timezone, tzfile format, Windows registry timezones, iCalendar timezones, and POSIX TZ strings.
class tzutc: ...
class tzoffset:
def __init__(self, name, offset): ...
class tzlocal: ...
class tzfile:
def __init__(self, fileobj, filename=None): ...
def gettz(name=None): ...
def enfold(dt, fold=1): ...
def datetime_ambiguous(dt, tz=None): ...
def datetime_exists(dt, tz=None): ...
UTC = tzutc() # Singleton instanceComputation of Easter Sunday dates using Western, Orthodox, and Julian calendar algorithms for any given year.
def easter(year, method=EASTER_WESTERN): ...
EASTER_JULIAN = 1
EASTER_ORTHODOX = 2
EASTER_WESTERN = 3General convenience functions for common date and time operations including current day calculation, timezone defaulting, and date comparison within tolerance.
def today(tzinfo=None): ...
def default_tzinfo(dt, tzinfo): ...
def within_delta(dt1, dt2, delta): ...Access to dateutil's bundled timezone database with metadata support, providing an alternative to system timezone data.
class ZoneInfoFile:
def __init__(self, zonefile_stream=None): ...
def get(self, name, default=None): ...
def get_zonefile_instance(new_instance=False): ...# From datetime module (used throughout dateutil)
class datetime: ...
class date: ...
class time: ...
class timedelta: ...
class tzinfo: ...
# dateutil-specific exceptions
class ParserError(ValueError): ...
class UnknownTimezoneWarning(RuntimeWarning): ...
class DeprecatedTzFormatWarning(Warning): ...