Peter
Peter

Reputation: 427

Python time and strftime

I am wanting to pass the current time as an object into a strftime reference. If I use:

d = date.fromtimestamp(time.time())

print d.strftime("%d-%b-%Y %H:%M:%S")

This will print out the time, but not include the minutes/hours etc:

04-Jul-2014 00:00:00 

If I try use a different call to try and get the minutes/hours:

d = date.fromtimestamp(time.localtime())

I get:

TypeError: a float is required

Both time.time() and time.localtime() appears to actually contain seconds/minutes etc, but not display them. Thoughts? gmtime also returns "AttributeError: 'time.struct_time' object has no attribute 'strftime'".

Upvotes: 0

Views: 2577

Answers (2)

wasw100
wasw100

Reputation: 41

You should use

from datetime import datetime

not

from datetime import date

eg.

import time
from datetime import datetime
d = datetime.fromtimestamp(time.time())
print d.strftime("%d-%b-%Y %H:%M:%S")

Upvotes: 1

sundar nataraj
sundar nataraj

Reputation: 8692

use datetime.fromtimestamp(time.time()) since date.fromtimestamp(time.time()) only draws date

d = datetime.fromtimestamp(time.time())
print d
print d.strftime("%d-%b-%Y %H:%M:%S")
#output 04-Jul-2014 11:32:40

Upvotes: 1

Related Questions