The datetime Module

datetime provides classes representing date and time objects, which can be either aware of time zones or naïve (the default). Class tzinfo is abstract: module datetime supplies no implementation (for all the gory details, see http://docs.python.org/lib/datetime-tzinfo.html). (See module pytz, in The pytz Module, for a good, simple implementation of tzinfo, which lets you easily create aware objects.) All the types in module datetime have immutable instances, so the instances’ attributes are all read-only and the instances can be keys in a dict or items in a set.

Class date

Class date instances represent a date (no time of day in particular within that date), are always naïve, and assume the Gregorian calendar was always in effect. date instances have three read-only integer attributes: year, month, and day.

date

date(year,month,day)

Class date also supplies some class methods usable as alternative constructors.

fromordinal

date.fromordinal(ordinal)

Returns a date object corresponding to the proleptic Gregorian ordinal ordinal, where a value of 1 corresponds to the first day of the year 1.

fromtimestamp

date.fromtimestamp(timestamp)

Returns a date object corresponding to the instant timestamp expressed in seconds since the epoch.

today

date.today( )

Returns a date object representing today’s date.

Instances of class date support some arithmetic: the difference between date instances is a timedelta instance, and you can add or subtract a date instance and a timedelta instance ...

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.