Python - 将秒从纪元时间转换为人类可读的时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26276906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:19:14  来源:igfitidea点击:

Python - Convert seconds from epoch time into human readable time

pythontime

提问by Daniel Hyuuga

Originally I made this code to convert date into human readable time:

最初我制作了这段代码来将日期转换为人类可读的时间:

    a = datetime.datetime.strptime(time, "%Y-%m-%d %H:%M:%S.%f")
    b = datetime.datetime.now()
    c = b - a
    days, hours, minutes, seconds = int(c.days), int(c.seconds // 3600), int(c.seconds % 3600 / 60.0), int(c.seconds % 60.0)
    return days, hours, minutes, seconds
    EXAMPLE OUTPUT: 1 days, 4 hours, 24 minutes, 37 seconds

and I'm trying to make it using epoch time, but I have no idea on to make it calculate days hours and etc.

我正在尝试使用纪元时间来制作它,但我不知道让它计算天数等。

    a = last_epoch #last epoch recorded
    b = time.time() #current epoch time
    c = b - a #returns seconds
    hours = c // 3600 / 24 #the only thing I managed to figure out

采纳答案by John Zwinck

a = last_epoch #last epoch recorded
b = time.time() #current epoch time
c = b - a #returns seconds
days = c // 86400
hours = c // 3600 % 24
minutes = c // 60 % 60
seconds = c % 60

回答by pyprism

import datetime
timestamp = 1339521878.04 
value = datetime.datetime.fromtimestamp(timestamp)
print(value.strftime('%Y-%m-%d %H:%M:%S'))