8

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?

  • 4
    Please take a moment to look at your previous questions and accept the answers (tick mark on the left), if they solved your problem. This way the question is marked as "answered", and the community can rely on the solution in the future, knowing it works. – ish Jun 28 '12 at 7:16
11

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)
| improve this answer | |
  • 1
    thank you very much, it is very useful to me and i satisfied now because of i learn one new thing today. – Viswa Jun 28 '12 at 8:43
  • 2
    Please mark the question as solved by clicking the tick-box to the left of my answer. – simme Jun 28 '12 at 11:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.