The dateutil Module

The third-party package dateutil (http://labix.org/python-dateutil) offers modules to manipulate dates in various ways, including time deltas, recurrence, time zones, and fuzzy parsing. (See the module’s web site for complete documentation of its rich functionality.) dateutil’s main modules are the following.

easter

easter.easter(year)

Returns the datetime.date object for Easter of the given year. For example:

from dateutil import easter
print easter.easter(2006)

emits 2006-04-16.

parser

parser.parse(s)

Returns the datetime.datetime object denoted by string s, with very permissive (a.k.a. “fuzzy”) parsing rules. For example:

from dateutil import parser
print parser.parse("Saturday, January 28, 2006, at
11:15pm")

emits 2006-01-28 23:15:00.

relativedelta

relativedelta.relativedelta(...)

You can call relativedelta with two instances of datetime.datetime: the resulting relativedelta instance captures the relative difference between the two arguments. Alternatively, you can call relativedelta with a keyword argument representing absolute information (year, month, day, hour, minute, second, microsecond); relative information (years, months, weeks, days, hours, minutes, seconds, microseconds), which may have positive or negative values; or the special keyword weekday, which can be a number from 0 (Monday) to 6 (Sunday), or one of the module attributes MO, TU,..., SU, which can also be called with a numeric argument n to specify the nth weekday. In any case, the resulting ...

Get Python in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.