如何将日期转换为时间段python

时间:2018-04-07 14:50:55

标签: python date nlp

用户案例: June 2016(OR 06/2016) => 06/01/2016-06/30/216 June 2016 to Dec 2016(OR between June 2016 and Dec 2016) => 06/01/2016-12/31/216

我想知道我是否可以从python调用python库或API服务,以便我可以将自然语言的日期转换为标准时间段,如上述用户案例

1 个答案:

答案 0 :(得分:1)

dateutil看起来应该有帮助。

<强>实施例

from dateutil import parser
from dateutil.relativedelta import relativedelta

s = 'June 2016(OR 06/2016)'
s = s.split("(OR")
start = parser.parse(s[0]).strftime("%m/01/%Y")
end = (parser.parse(start) + relativedelta(day=31)).strftime("%m/%d/%Y")
print "{0}-{1}".format(start, end)

<强>输出:

06/01/2016-06/30/2016