Displaying the time

2.1k Views Asked by At

So, using this website i have come up with the following code:

import time
print(time.strftime('%l:%M%p %Z on %b %d %Y'))
#' 1:36PM EDT on Oct 18, 2010'
print("time done")
time.sleep(5)

From the following sources: 1,2

For some reason though, this code doesn't work.

I'm trying to get the time to show similar to the comment, although if I could get the seconds in as well that would be good. I also tried this:

import time
print time.strftime("%l"+":"+"%M"+"%p"+ "%Z"+ "%b"+ "%d"+ "%Y")
#' 1:36PM EDT on Oct 18, 2010'
print("time done")
time.sleep(5)

but it doesn't work either.

EDIT: Error I get even from print(time.strftime('%l:%M:%S%p %Z on %b %d, %Y')):

AttributeError: '<invalid type>' object has no attribute 'strftime'

EDIT2: for those that don't believe me: !The error and code in CodeSkulptor] https://www.dropbox.com/s/joyxsod8yv899fm/THERE.png

I used python 3.3.3 and codeskulptor

1

There are 1 best solutions below

9
On

All you forgot was a comma after %d:

>>> print(time.strftime('%l:%M%p %Z on %b %d, %Y'))
 7:06PM GMT on Dec 14, 2013

Adding in seconds means you want to insert %S somewhere:

>>> print(time.strftime('%l:%M:%S%p %Z on %b %d, %Y'))
 7:07:47PM GMT on Dec 14, 2013

Note that CodeSkulptor does not support the time module very well. Code using time.strftime() on CodeSkulptor will fail, but that is hardly a Python problem. Even a base print(time.strftime()) call fails.