How to use date object

Andrew Dalke adalke at mindspring.com
Sat Oct 23 05:04:21 CEST 2004


marie wrote:
> I am a newbie in python.  And I would like to know how to create and
> use the methods in date.  Can someone please show me some code about
> it.  Thanks.

The documentation for the datetime module is at
   http://python.org/doc/2.3/lib/module-datetime.html

Some examples are in the Python Cookbook at
   http://aspn.activestate.com/ASPN/Python/Cookbook/

but they aren't that useful as an intro / tutorial.

Here's some examples:

 >>> import datetime
 >>> today = datetime.date.today()
 >>> print today
2004-10-22
 >>> now = datetime.datetime.now()
 >>> print now
2004-10-22 20:58:30.308608
 >>> tomorrow = today + datetime.timedelta(days = 1)
 >>> print tomorrow
2004-10-23
 >>> now.hour
20
 >>> now.minute
58
 >>> now.second
30
 >>> now.microsecond
308608
 >>> today.year
2004
 >>> today.month
10
 >>> today.day
22
 >>> moonwalk = datetime.date(1969, 7, 20)
 >>> today - moonwalk
datetime.timedelta(12878)
 >>> delta = today - moonwalk
 >>> delta.days
12878
 >>>


				Andrew
				dalke at dalkescientific.com





More information about the Python-list mailing list