# -*- coding: utf-8 -*-
"""
Created on Tue Jan 8 18:02:03 2019
@author: liuty
@e-mail: liuty66@163.com
@introduction:
解析时间模块
"""
str1=' 2019-01-08'
str2='2019-1-8 00:00:00'
str3='2019年1月8日'
str4='2019年01月08日'
str5='2019/1/8'
str6='2019/01/08 00:00'
str7='2019-1-8'
def str2date(str_date):
str_date=str_date.strip()
year=1900
month=1
day=1
if(len(str_date)>11):
str_date=str_date[:11]
if(str_date.find('-')>0):
year=str_date[:4]
if(year.isdigit()):
year=int(year)
else:
year=0
month=str_date[5:str_date.rfind('-')]
if(month.isdigit()):
month=int(month)
else:
month=0
if(str_date.find(' ')==-1):
day=str_date[str_date.rfind('-')+1:]
else:
day=str_date[str_date.rfind('-')+1:str_date.find(' ')]
if(day.isdigit()):
day=int(day)
else:
day=0
elif(str_date.find('年')>0):
year=str_date[:4]
if(year.isdigit()):
year=int(year)
else:
year=0
month=str_date[5:str_date.rfind('月')]
if(month.isdigit()):
month=int(month)
else:
month=0
day=str_date[str_date.rfind('月')+1:str_date.rfind('日')]
if(day.isdigit()):
day=int(day)
else:
day=0
elif(str_date.find('/')>0):
year=str_date[:4]
if(year.isdigit()):
year=int(year)
else:
year=0
month=str_date[5:str_date.rfind('/')]
if(month.isdigit()):
month=int(month)
else:
month=0
if(str_date.find(' ')==-1):
day=str_date[str_date.rfind('/')+1:]
else:
day=str_date[str_date.rfind('/')+1:str_date.find(' ')]
if(day.isdigit()):
day=int(day)
else:
day=0
else:
year=1900
month=1
day=1
if month<10:
month='0'+str(month)
if day<10:
day='0'+str(day)
return '%s-%s-%s' % (year,month,day)
print(str2date('2017-12-22 a'))
————————————————
版权声明:本文为CSDN博主「liuty66」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/liuty66/article/details/86091906
02-17
1952
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)