Python中有关时间的计算(datetime)

Python中有关时间的计算(datetime)

1.从字符串中取时间并以datetime格式存储

import datetime

#字符串形式存储的时间数据
Time = "2018-08-08 22::56:03"

#从字符中提取
TimeValue = datetime.datetime.strptime(Time, "%Y-%m-%d %H:%M:%S")

print(TimeValue)

2.datetime时间相加减

import datetime
Time1 =  "2018-08-08 22::56:03"
Time2 =  "2018-08-09 22:56:03"

TimeValue1 = datetime.datetime.strptime(Time1, "%Y-%m-%d %H:%M:%S")
TimeValue2 = datetime.datetime.strptime(Time2, "%Y-%m-%d %H:%M:%S")

TimeDifference = TimeValue2 - TimeValue1

#输出时间差的秒数
print(TimeDifference.seconds)
#输出差的天数
print(TimeDifference.days)

print(TimeDifference)
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页