python date str时间互转
```python
import datetime
import math
import time
#获取当前时间 datetime.datetime类型
receivedTime = datetime.datetime.strptime(time.strftime('%Y-%m-%d %H:%M:%S'), '%Y-%m-%d %H:%M:%S')
print(receivedTime,type(receivedTime))
#2020-09-04 15:28:40 <class 'datetime.datetime'>
#获取当前时间 str类型
now_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
print(now_time,type(now_time))
#2020-09-04 15:28:40 <class 'str'>
#获取当前时间
now = datetime.datetime.now()
print(now,type(now))
#2020-09-04 15:31:19.207993 <class 'datetime.datetime'>
#str转为datetime.datetime类型
std = '2020-09-04 06:55:00'
dateTime = datetime.datetime.strptime(std,'%Y-%m-%d %H:%M:%S')
t = (dateTime + datetime.timedelta(hours=-2))
print(t,type(t))
#2020-09-04 04:55:00 <class 'datetime.datetime'>
#datetime 转为str
dateStr = t.strftime('%Y-%m-%d %H:%M:%S')
print(dateStr,type(dateStr))
#2020-09-04 04:55:00 <class 'str'>
#时间相加减
distance = receivedTime - t
seconds = distance.total_seconds() / 60
m = math.ceil(seconds)
print(distance,type(distance))
print(m,type(m))
#10:51:22 <class 'datetime.timedelta'>
#666 <class 'int'>
def of_stamp(timeStamp):
"""
时间戳转换为指定字符串时间格式
:param timeStamp:
:return:
"""
stamp = timeStamp / 1000
timeArray = time.localtime(stamp)
otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
return otherStyleTime
def delta_time(timeStamp, timeDelta):
"""
时间加减计算,并转换为指定字符串格式
:param timeStamp:
:param timeDelta:
:return:
"""
timestamp = timeStamp / 1000
datetime_struct = datetime.datetime.fromtimestamp(timestamp)
datetime_obj = (datetime_struct + datetime.timedelta(minutes=timeDelta))
datetime_str = datetime_obj.strftime('%Y/%m/%d %H:%M:%S')
return datetime_str