代码语言:javascript复制
import datetime,re
def parse_date(date_str):
try:
if not date_str:
return None
if "-" in date_str:
if date_str.count("-") == 1:
date = datetime.datetime.strptime(date_str, "%Y-%m")
elif date_str.count("-") == 2:
date = datetime.datetime.strptime(date_str, "%Y-%m-%d")
elif "年" in date_str:
if "日" in date_str:
date = datetime.datetime.strptime(date_str, "%Y年%m月%d日")
elif "月" in date_str:
date = datetime.datetime.strptime(date_str, "%Y年%m月")
else:
date = datetime.datetime.strptime(date_str, "%Y年")
elif date_str.isdigit():
if len(date_str) == 4:
date = datetime.datetime.strptime(date_str, "%Y")
elif len(date_str) > 6:
date = datetime.datetime.strptime(date_str, "%Y%m%d")
else:
date = datetime.datetime.strptime(date_str, "%Y%m")
else:
date = None
except:
return None
return date
date_reg_v1 = re.compile(r"d 年d 月d 日")
date_reg_v2 = re.compile(r"d 年d 月")
date_reg_v3 = re.compile(r"d -d -d ")
date_reg_v4 = re.compile(r"d -d ")
str =' 2199年10月 '
# 正则去掉多余的
kk = date_reg_v2.search(str).group()
kk =parse_date(kk)
print(kk)
结果 :
2199-10-01 00:00:00