Skip to content

Instantly share code, notes, and snippets.

@dtanham
Created December 27, 2013 23:22
Show Gist options
  • Save dtanham/8154025 to your computer and use it in GitHub Desktop.
Save dtanham/8154025 to your computer and use it in GitHub Desktop.
import parsedatetime.parsedatetime as pdt
def datetimeFromString( s ):
c = pdt.Calendar()
result, what = c.parse( s )
dt = None
# what was returned (see http://code-bear.com/code/parsedatetime/docs/)
# 0 = failed to parse
# 1 = date (with current time, as a struct_time)
# 2 = time (with current date, as a struct_time)
# 3 = datetime
if what in (1,2):
# result is struct_time
dt = datetime.datetime( *result[:6] )
elif what == 3:
# result is a datetime
dt = result
if dt is None:
# Failed to parse
raise ValueError, ("Don't understand date '"+s+"'")
return dt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment