def date_check(date_str): now = datetime.date.today() zf_list = { '一': 1, '二': 2, '两': 2, '三': 3, '四': 4, '五': 5, '六': 6, '七': 7, '八': 8, '九': 9, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9 } t = None if date_str.find('今天') >= 0: delta = datetime.timedelta(days=0) t = now - delta elif date_str.find('昨天') >= 0: delta = datetime.timedelta(days=1) t = now - delta elif date_str.find('前天') >= 0: delta = datetime.timedelta(days=2) t = now - delta elif date_str.find('天前') >= 0: day = re.findall(r'(?P<day>.*?)天前', date_str) if len(day) > 0 and day[0] in zf_list.keys(): delta = datetime.timedelta(days=zf_list[day[0]]) t = now - delta else: t = now elif date_str.find('周前') >= 0: week = re.findall(r'(?P<week>.*?)周前', date_str) if len(week) > 0 and week[0] in zf_list.keys(): delta = datetime.timedelta(weeks=zf_list[week[0]]) t = now - delta else: t = now else: r = re.findall(r'(?P<year>\d*?)年', date_str) if len(r) > 0: year = int(r[0]) else: year = now.year r = re.findall(r'(?P<m>.)月(?P<d>.)日', date_str) if len(r) > 0: month = int(r[0][0]) day = int(r[0][1]) else: month = now.month day = now.day t = datetime.date(year, month, day) return t.strftime('%Y-%m-%d %H:%M:%S')
评论