次の日付パターンを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')