O'Reilly logo

Python in a Nutshell, 2nd Edition by Alex Martelli

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required