美文网首页
python格式化时间段

python格式化时间段

作者: 朝西的生活 | 来源:发表于2017-09-20 14:14 被阅读0次
def td_format(td_object):
    seconds = int(td_object.total_seconds())
    periods = [
        ('year', 60*60*24*365),
        ('month', 60*60*24*30),
        ('day', 60*60*24),
        ('hour', 60*60),
        ('minute', 60),
        ('second', 1)
    ]
    result = {}
    for period_name,period_seconds in periods:
        if seconds> period_seconds:
            period_value, seconds = divmod(seconds,period_seconds)
            result[period_name] = period_value
        else:
            result[period_name] = 0
    return result

相关文章

  • python格式化时间段

  • python基础知识(3)

    python字符串 python转义字符 python字符串运算符 python字符串格式化 python格式化操...

  • 字符串的格式化操作

    旧式字符串格式化%运算符,位置格式化(python2) str.format字符串格式化(python3,它存在一...

  • python2,3的小秘密

    Python字符串格式化,有两个版本 1.Python2.5之前,我们使用的使用老式的格式化 2.Python3....

  • 实战

    python的格式化输出 #python格式化输出 ##%对于未知变量类型,用这样就不太方便了 name='lis...

  • 格式化Curl返回的Json字符

    格式化Curl返回的Json字符 格式化Curl返回的Json字符Python 格式化Nodejs 格式化 经常会...

  • Python学习笔记-3群18组-杜杜狼-2017.7.26

    在昨天的学习中发现自己对python的格式化还不明白,今天专门研究一下格式化。 Python格式化学习 Pytho...

  • 4.2 Python

    4.2.1. 格式化字符串   在Python中,有两种格式化字符串的方式,在Python2的较低版本中,格式化字...

  • 013.Python格式化

    Python格式化 1. 概述 Python2.6 开始,新增了一种格式化字符串的函数 str.format(),...

  • format() 格式化函数

    Python format 格式化函数 | 菜鸟教程

网友评论

      本文标题:python格式化时间段

      本文链接:https://www.haomeiwen.com/subject/bvqosxtx.html