How to convert the time.time() to normal datetime format in linux? [closed]

I can convert the current date and time in seconds by using time.time() like

now = time.time()
print("the now time is ") + str(now)

Output

the now time is 1340867411.88

is there any command to change this 1340867411.88 to current date time format?


Solution 1:

From the looks of your code I assume that you are using this for a python script. Because of this, we'll have to proceed this way:

 import time
 now = time.ctime(int(time.time()))
 print("the now time is ") + str(now)

Which, using your example timestamp, should give you the following output:

 Thu, 28 Jun 2012 07:10:11 GMT

However, I would just go ahead and use ctime instead of time to avoid having to convert the timestamp.

If you'd like only the time, you could use this approach instead:

 import time
 now = time.strftime("%H:%M", time.localtime(time.time()))
 print("the now time is ") + str(now)