-3

How do I convert these strings:

one hour ago
three days ago
two weeks ago
yesterday
next month

into Python datetime object?

flowfree
  • 16,356
  • 12
  • 52
  • 76

1 Answers1

1

Just found parsedatetime for parsing human readable date/time text from the link provided by Jon Clements. Here is a solution in case you interested:

from time import mktime
from datetime import datetime
import parsedatetime as pdt

time_str = '1 hour ago'
cal = pdt.Calendar()
dt = datetime.fromtimestamp(mktime(cal.parse(time_str)[0]))
time_formatted = dt.strftime('%b %d, %Y %H:%M')
print(time_formatted) # will print something like: Dec 15, 2013 02:10

Also see this question: python 'x days ago' to datetime

Community
  • 1
  • 1
flowfree
  • 16,356
  • 12
  • 52
  • 76