faqts : Computers : Programming : Languages : Python : Modules : time

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

47 of 50 people (94%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How do I print out the current time?
How can I get the current date and time?

Jun 22nd, 2000 05:04
unknown unknown, Mike Hostetler, Jeff


I didn't think that the official documentation was very clear on this 
(but it is there), so I'm adding it here.

If you do

>> import time
>> print time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))

You'll see something like:

2000-06-08 15:05:09

To dissect this:

time.time() gets the time in seconds since the epoch

time.localtime(secs) creates a tuple

time.strftime(string,tuple) creates a string of the time, using the 
formatting in "string" (that is what the "%"'s are doing there).
To see the full table of formatting options, see 
http://www.python.org/doc/current/lib/module-time.html

Also, the system man page, ('man strftime')? documents a closer set of 
the formatting options actually supported by the libc, which is what the 
python interpreter actually calls into. this will come into play mostly 
on 'older' machines, but python runs on them too!