1

As part of the assignment, I am asked to find current time from total number of seconds passed since 1st Jan 1970. I know we can use time.time() to get the seconds but I am having trouble getting into current time.

Attached is the code I have so far.

import time
a=time.time()
hours= (a)//3600
minutes=(a-hours*3600)/60
second=a%60
print(hours,"h",minutes,"m",second,"s")

Please advise.

Johan
  • 5,003
  • 3
  • 36
  • 50

3 Answers3

2
import time
current_time = time.ctime(int(time.time()))
print ("current_time:") + str(current_time)

output: Thu, 28 Jun 2012 07:10:11 GMT

for only time use this:

 import time
 current_time = time.strftime("%H:%M", time.localtime(time.time()))
 print("current_time:") + str(current_time)
Prasanna
  • 96
  • 8
1

If you are looking for overall time passed since 1970 in HH:MM:SS format you can try this

import time
t=time.time()
print "total second from 1970 :" ,t
m, s = divmod(t, 60)
h, m = divmod(m, 60)
print "Overall time passed since 1970: ","%d:%02d:%02d" % (h, m, s)

And you are just looking for current time in HH.MM.SS, use strftime like this.

from datetime import datetime
t = datetime.now().strftime("%H:%M:%S")
print "Current Time is :",t
Shivkumar kondi
  • 6,458
  • 9
  • 31
  • 58
0
import time
current_time = time.ctime()
print("current time is:",current_time[11:19])
Josh Anish
  • 101
  • 1
  • 5