将以下日期模式datetime.date
转换为Python中的对象的最佳方法是什么?
1st April
8th April
15th April
1st May
dateutil.parser
将解析几乎所有已知的日期格式:
from dateutil.parser import parse
parse('8th April')
要始终获得未来的日期:
from dateutil.parser import parse
from datetime import datetime
d = parse('8th April')
if (d - datetime.now()).days <= 0:
d = datetime.date(d.year+1, d.month, d.day)
from datetime import datetime
val = val.replace('th', '').replace('st', '')
d = datetime.strptime(val, '%d %B')