%20%E8%BD%AC%E6%8D%A2%E4%B8%BA%E6%AD%A3%E5%B8%B8%E6%97%A5%E6%9C%9F%E6%97%B6%E9%97%B4%E6%A0%BC%E5%BC%8F%EF%BC%9F.png)
time.time()
我可以使用类似方法将当前日期和时间转换为秒数
now = time.time()
print("the now time is ") + str(now)
输出
the now time is 1340867411.88
是否有任何命令可以将其更改1340867411.88
为当前日期时间格式?
答案1
从代码来看,我假设您正在将其用于 Python 脚本。因此,我们必须按如下方式进行:
import time
now = time.ctime(int(time.time()))
print("the now time is ") + str(now)
使用您的示例时间戳,应该会给您以下输出:
Thu, 28 Jun 2012 07:10:11 GMT
不过,我将继续使用 ctime 而不是 time,以避免转换时间戳。
如果您只想要时间,则可以使用此方法:
import time
now = time.strftime("%H:%M", time.localtime(time.time()))
print("the now time is ") + str(now)