python对时间的操作

python查询当天是今年的第几周

import time
print (time.strftime("%W"))

结果就是第几周,是个str类型,而不是int类型,大家使用的时候要注意下类型

秒数转成相应的周、天、小时、分钟

def display_time(seconds):
    result = {}
    intervals = (
        ('weeks', 604800),  # 60 * 60 * 24 * 7
        ('days', 86400),  # 60 * 60 * 24
        ('hours', 3600),  # 60 * 60
        ('minutes', 60),
        ('seconds', 1),
    )
    for name, count in intervals:
        value = seconds // count
        if value:
            seconds -= value * count
            if value == 1:
                name = name.rstrip('s')
            result[name] = value
    return result

time_long = 4444591.2265775
print(display_time(time_long))
结果样式:

{'weeks': 7.0, 'days': 2.0, 'hours': 10.0, 'minutes': 36.0, 'seconds': 31.0}
发布了79 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/s_daqing/article/details/104938166