How can I translate dates and times from natural language to datetime? [closed]

parsedatetimePython module that is able to parse ‘human readable’ date/time expressions.

#!/usr/bin/env python
from datetime import datetime
import parsedatetime as pdt # $ pip install parsedatetime

cal = pdt.Calendar()
now = datetime.now()
print("now: %s" % now)
for time_string in ["tomorrow at 6am", "next moday at noon", 
                    "2 min ago", "3 weeks ago", "1 month ago"]:
   print("%s:\t%s" % (time_string, cal.parseDT(time_string, now)[0]))

Output

now: 2015-10-18 13:55:29.732131
tomorrow at 6am:    2015-10-19 06:00:00
next moday at noon: 2015-10-18 12:00:00
2 min ago:  2015-10-18 13:53:29
3 weeks ago:    2015-09-27 13:55:29
1 month ago:    2015-09-18 13:55:29

Leave a Comment