python 爬虫数据处理字符串时间转换格式方法
startDate = "2018-10-01"
endDate = "2018-10-31"
###字符转化为日期
startTime = datetime.datetime.strptime(startDate, '%Y-%m-%d').time()
endTime = datetime.datetime.strptime(endDate, '%Y-%m-%d').time()
now = datetime.datetime.now()
print(now)
###日期转化为字符串
print("--1---:" + datetime.datetime.strftime(startTime, "%Y-%m-%d"))
print("--2---:" + datetime.datetime.strftime(endTime, "%Y-%m-%d"))
#String to Date(datetime)
def stringToDate(string):
#example '2013-07-22 09:44:15+00:00'
dt = datetime.strptime(string, "%Y-%m-%d %H:%M:%S+00:00")
#print dt
return dt
#Date(datetime) to String
def dateToString(date):
ds = date.strftime('%Y-%m-%d %H:%M:%S')
return ds
#return n hours after datetime
def getAfterDate(n):
dnow = datetime.datetime.now()
dafter = dnow + datetime.timedelta(hours=n)
#dafter.ctime()
return dafter
---------------------
1⃣️str -> date
import datetime
detester = ‘2017-01-01'
date = datetime.datetime.strptime(detester,’%Y-%m-%d')1234
2⃣️date -> str
import datetime
date = datetime.now()
detester = date.strftime(‘%Y-%m-%d')
---------------------
import time