Pythomania
Pythomania

Reputation: 658

Python: What is the easiest way to convert from time.time() value to time.strftime() value?

I need to represent the time in ("%m/%d/%Y %H:%M:%S") format from the float value I am getting in the form of time.time().

I already have a value that is in the form of time.time(). For example I already have a value I am getting every .3 seconds from a device which is say, 1332449493.0, ...., I would then like to convert 1332449493.0 to a strftime format.

EX: time.strftime("%m/%d/%Y %H:%M:%S", 1332449493.0) will throw a TypeError because strf requires a 9-item tuple and not a float.

Upvotes: 1

Views: 1805

Answers (2)

Andreas Florath
Andreas Florath

Reputation: 4612

time.strftime("%m/%d/%Y %H:%M:%S", time.localtime(1332449493.0))

does the job.

Upvotes: 7

Ismail Badawi
Ismail Badawi

Reputation: 37197

You can easily get a datetime from it:

datetime.datetime.fromtimestamp(time.time())

Then you can format it however you like.

Upvotes: 0

Related Questions