t='November 27th, 2022' import dateparser from dateutil.parser import parse import locale def date_change(temp): temp = str(temp) locale.setlocale(locale.LC_TIME, 'en_US') ts = ['%b %d, %Y', '%m-%d-%Y', '%d %B %Y', '%d %b %Y', '%Y-%m-%d', '%Y/%m/%d', '%y/%m/%d', '%d-%b-%Y', '%d-%b-%y', '%Y/%m/%d %H:%M', '%Y/%m/%d %H:%M:%S', '%Y-%m-%d %H:%M:%S', '%m %d, %Y', '%d. %b %Y','%d %m %Y'] day_dic = {'一月': 1, '二月': 2, '三月': 3, '四月': 4, '五月': 5, '六月': 6, '七月': 7, '八月': 8, '九月': 9, '十月': 10, '十一月': 11, '十二月': 12} if day_dic.get(temp.split('月')[0] + '月'): temp = temp.replace(temp.split('月')[0] + '月', str(day_dic[temp.split('月')[0] + '月'])) flag = False temp = temp.replace('月', '') for i in ts: try: flag = datetime.strptime(temp, i).strftime('%Y-%m-%d') break except:pass try: flag = parse(temp).strftime('%Y-%m-%d') except:pass if flag: return flag else: temp = dateparser.parse(temp).strftime('%Y-%m-%d') return temp print(date_change(t))
python 日期格式转换
最新推荐文章于 2023-11-21 17:12:12 发布