须知

通常的时间戳是从1970-01-01 00:00:00开始算的

格式化操作

默认时间格式修改

datetime.datetime(2020, 12, 10, 14, 55, 36, 705048) --> '2020-12-10 14:55:24'

now=datetime.datetime.now()
now.strftime('%Y-%m-%d %H:%M:%S')
时长格式化

比如:52950 -> 00:55:24

def ms2hms(ms):
    import time
    tt = '1970-01-01 00:00:00'
    timeStampt = time.mktime(time.strptime(tt, "%Y-%m-%d %H:%M:%S"))
    timeStamp = timeStampt + int(ms % 1000)
    timeArray = time.localtime(timeStamp)
    otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
    res = otherStyleTime.split(' ')[1]
    return res
格式化的时间时长统计

比如:00:55:24 --> 52950

def hms_tm(stm):
    hms = stm.split(":")
    total = 0
    try:
        total += int(hms[0])*3600
        total += int(hms[1])*60
        total += int(hms[2])
    except:
        return 0

    return total

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐