簡體   English   中英

在python中以時間hh:mm:ss的形式將time.time()秒轉換為當前時間

[英]Covert time.time() seconds into current time with format hh:mm:ss in python

作為作業的一部分,要求我從1970年1月1日以來經過的總秒數中查找當前時間。我知道我們可以使用time.time()來獲取秒數,但是我很難進入當前時間。

隨附的是我到目前為止的代碼。

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

請指教。

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

輸出:2012年6月28日,星期四,格林尼治標准時間

只能使用此時間:

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

如果您正在尋找自1970年以來采用HH:MM:SS格式的總時間,則可以嘗試以下方法

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)

而且您只是在尋找HH.MM.SS中的當前時間,使用這樣的strftime。

from datetime import datetime
t = datetime.now().strftime("%H:%M:%S")
print "Current Time is :",t
import time
current_time = time.ctime()
print("current time is:",current_time[11:19])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM