1

将以下日期模式datetime.date转换为Python中的对象的最佳方法是什么?

1st April
8th April
15th April
1st May
4

2 回答 2

4

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)
于 2012-04-12T18:22:51.493 回答
0
from datetime import datetime

val = val.replace('th', '').replace('st', '')
d = datetime.strptime(val, '%d %B')
于 2012-04-12T18:21:36.367 回答