Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I strtotime in python?

I'm scraping a a page that includes among other things, date information. So I have a variable named warrant_issued that contains u'11/5/2003' -- I want to store this as a machine readable date. PHP has a handy strtotime function that works fabulously. I was hoping that datetime's strptime would help me but it doesn't seem to be available in my version of datetime -- here is everything in my tab complete on datetime.

In [231]: datetime.
datetime.MAXYEAR           datetime.__hash__          datetime.__sizeof__
datetime.MINYEAR           datetime.__init__          datetime.__str__
datetime.__class__         datetime.__name__          datetime.__subclasshook__
datetime.__delattr__       datetime.__new__           datetime.date
datetime.__dict__          datetime.__package__       datetime.datetime
datetime.__doc__           datetime.__reduce__        datetime.datetime_CAPI
datetime.__file__          datetime.__reduce_ex__     datetime.time
datetime.__format__        datetime.__repr__          datetime.timedelta
datetime.__getattribute__  datetime.__setattr__       datetime.tzinfo

I'm using iPython 2.7.2+

Am I barking up the wrong tree here? What's the best way to turn u'11/5/2003' into a date?

like image 920
Amanda Avatar asked Nov 25 '12 00:11

Amanda


People also ask

How do I convert string to time in Python?

We can convert a string to datetime using strptime() function. This function is available in datetime and time modules to parse a string to datetime and time objects respectively.

How do I convert Strtotime to date format?

Code for converting a string to dateTime$date = strtotime ( $input ); echo date ( 'd/M/Y h:i:s' , $date );

How do you parse a date in Python?

Python has a built-in method to parse dates, strptime . This example takes the string “2020–01–01 14:00” and parses it to a datetime object. The documentation for strptime provides a great overview of all format-string options.


1 Answers

strptime() is definitely the right approach, it's just a class method for the datetime class (confusingly part of the datetime module).

That is, datetime.datetime.strptime() is what you're looking for (and not datetime.strptime().

like image 100
David Cain Avatar answered Sep 26 '22 02:09

David Cain