欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

计算某时间戳与当前时间的时间差,输出格式为天时分秒

程序员文章站 2024-01-22 19:48:52
...
def timediff_to_str(timestamp):
    onlineTime = datetime.datetime.fromtimestamp(timestamp)
    localTime = datetime.datetime.now()
    result = localTime - onlineTime
    hours = int(result.seconds / 3600)
    minutes = int(result.seconds % 3600 / 60)
    seconds = result.seconds%3600%60
    return "{0}天{1}时{2}分{3}秒".format(result.days,hours,minutes,seconds)