help with "date" :-) ?

Derek Thomson derek at wedgetail.com
Sun Dec 29 03:08:02 EST 2002


Adolfo wrote:

>
> I am looking to get the current date out of my machine. I am running a
> scrip file that asks for input, and then should make calculations
> using the current date as a base. I am teaching myself Python on
> Windows ME.
>
> Any help with this subject will be much apreciated,



Did you take a look at the "time" module? It provides a range of 
functions for getting the current time and date in various formats.

http://python.org/doc/current/lib/module-time.html

For date calculations, you probable want to use "localtime", which gives 
you the time broken up into year, month, day, hour, minute, second etc. 
It's all described in the above HTML document.

You can also get the time as seconds since the epoch ("time"), which is 
also very useful for performing date and time manipulations.

Here's a quick example of "localtime" and "asctime".

#!/usr/bin/env python

import time;

#
# Get the current date and time as a tuple
#

now = time.localtime()

print 'Year:', now[0], 'Month:', now[1], 'Day:', now[2]
print 'Hour:', now[3], 'Minute:', now[4], 'Second:', now[5]
print 'Weekday:', now[6], 'Julian day:', now[7]
print 'Daylight savings flag:', now[8]

print # Separator

#
# Get the current date and time as a string
#

print time.asctime()




More information about the Python-list mailing list