import datetime as DT
import dateutil.parser as DP
today = DT.datetime(2016, 4, 13)
for text in ('today', 'tomorrow', 'this Sunday', 'Wednesday next week',
'next week Wednesday',
'next thursday', 'next tuesday in June', '11/28',
'Concert this Saturday'
"lunch with Andrew @ Mon Mar 7, 2016",
'meeting on Tuesday, 3/29'):
dp_date = DP.parse(text, default=today, fuzzy=True)
print('{:35} > {}'.format(text, dp_date))
收益率
today > 2016-04-13 00:00:00
tomorrow > 2016-04-13 00:00:00 should be 2016-04-14
this Sunday > 2016-04-17 00:00:00
Wednesday next week > 2016-04-13 00:00:00
next week Wednesday > 2016-04-13 00:00:00
next thursday > 2016-04-14 00:00:00
next tuesday in June > 2016-06-14 00:00:00 should be 2016-06-07
11/28 > 2016-11-28 00:00:00
Concert this Saturday > 2016-04-16 00:00:00
lunch with Andrew @ Mon Mar 7, 2016 > 2016-03-07 00:00:00
meeting on Tuesday, 3/29 > 2016-03-29 00:00:00
dateutil.parser.parse
接受default
参数,可用于指定引用日期:收益率
但是,请注意,并非所有短语都能正确解析。你知道吗
相关问题 更多 >
编程相关推荐