views:

96

answers:

4

How can I display the current time as:

12:18PM EST on Oct 18, 2010

in Python. Thanks.

+1  A: 

Take a look at the facilities provided by http://docs.python.org/library/time.html

You have several conversion functions there.

Edit: see datetime (http://docs.python.org/library/datetime.html#module-datetime) also for more OOP-like solutions. The time library linked above is kinda imperative.

Santiago Lezica
+2  A: 

All you need is in the documentation.

import time
time.strftime('%X %x %Z')
'16:08:12 05/08/03 AEST'
Aif
+2  A: 

You could do something like:

>>> from time import gmtime, strftime
>>> strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
'Thu, 28 Jun 2001 14:17:15 +0000'

The full doc on the % codes are at http://docs.python.org/library/time.html

Thomas Ahle
+1  A: 

First the quick and dirty way, and second the precise way (recognizing daylight's savings or not).

import time
time.ctime() # 'Mon Oct 18 13:35:29 2010'
time.strftime('%l:%M%p %Z on %b %d, %Y') # ' 1:36PM EDT on Oct 18, 2010'
time.strftime('%l:%M%p %z on %b %d, %Y') # ' 1:36PM EST on Oct 18, 2010'
jimbob