python-dateutil
Extensions to the datetime module with powerful parsing, relative deltas, and timezone lookups.
Official Documentation ↗v2.9. Check the official docs for the latest API surface.Installation
pip install python-dateutilMethods
No methods match this filter.
Extensions to the datetime module with powerful parsing, relative deltas, and timezone lookups.
Overview
python-dateutil fills the gaps datetime left behind: a parser that accepts nearly any format people actually produce, calendar-aware deltas, recurrence rules, and IANA timezone resolution off the system database.
parser.parse with explicit dayfirst and yearfirst flags tames ambiguous inputs; relativedelta makes month and weekday arithmetic correct; rrule expresses schedules from cron-like frequencies; and tz.gettz replaces hand-built offset tables.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
parser.parse— Parses nearly any human-written date string into a datetime, tolerating unknown formats.relativedelta— Calendar-aware arithmetic: months of varying length, weekday snapping, and mixed units.rrule— Generates recurrence rules, daily, weekly patterns, Nth weekdays, producing datetime sequences.tz.gettz— Resolves IANA timezone names into tzinfo objects using the system database, no pytz needed.
Usage Notes
- Push external inputs to ISO 8601 at boundaries; use parser only where formats genuinely vary.
- Prefer relativedelta for anything involving months, years, or weekdays.
- Store aware datetimes in UTC and convert for display with tz.gettz.
Common Pitfalls
- Ambiguous numeric dates parse silently with the wrong day/month order without dayfirst.
- replace(tzinfo=…) reinterprets wall-clock time instead of converting; use astimezone.
- rrule with both count and until stops at whichever limit hits first.
FAQ
- Why is 01/02/2026 ambiguous?
- Dateutil defaults to month-first (US) reading. Pass dayfirst=True for the majority of the world or yearfirst where applicable, and ideally normalize inputs to ISO 8601 at the boundary instead.
- relativedelta versus timedelta?
- timedelta counts exact seconds and ignores the calendar, so one month becomes 30 days and Jan 31 + 1 month overflows. relativedelta works in calendar units, clamping month-end correctly and supporting weekday logic.
- Does parse trust timezones in input?
- If the string carries an offset, parse returns an aware datetime; otherwise you get a naive one, and assuming a zone afterwards with replace(tzinfo=...) uses wall-clock time. Attach zones with astimezone or tz-localize logic deliberately.
Report a problem with this API docOpens a pre-filled issue on GitHub.