编程语言
首页 > 编程语言> > python解析时间

python解析时间

作者:互联网

>>> strtime="20210702155822065"
>>> strtime2="20210702155821729"
>>> from datetime import datetime

#字符串解析成datetime
>>> datetime.strptime(strtime,"%Y%m%d%H%M%S%f")
datetime.datetime(2021, 7, 2, 15, 58, 22, 65000)
>>> datetime.strptime(strtime2,"%Y%m%d%H%M%S%f")
datetime.datetime(2021, 7, 2, 15, 58, 21, 729000)
>>> d1=datetime.strptime(strtime,"%Y%m%d%H%M%S%f")
>>> d2=datetime.strptime(strtime2,"%Y%m%d%H%M%S%f")

#计算时间差
>>> print d1-d2
0:00:00.336000
>>> print type(d1-d2)
<type 'datetime.timedelta'>
>>> print type(d1)   
<type 'datetime.datetime'>

#打印时间差,专程微秒级int类型
>>> (d1-d2).seconds
0
>>> (d1-d2).microseconds
336000
>>> type((d1-d2).microseconds)
<type 'int'>

标签:M%,python,d%,datetime,时间,解析,d2,S%,d1
来源: https://blog.csdn.net/zhuxian2009/article/details/118421574