简体   繁体   中英

How do I strtotime in python?

I'm scraping aa 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?

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() .

Try this:

For use with the datetime module , documentation here

>>>import datetime
>>>a = u'11/5/2003'
>>>time1 = datetime.datetime.strptime(a, "%m/%d/%Y")
>>>print time1
datetime.datetime(2003, 11, 5, 0, 0)

In ipython :

In [1]: import datetime

In [2]: a = u'11/5/2003'

In [3]: time1 = datetime.datetime.strptime(a, "%m/%d/%Y")

In [4]: print time1
2003-11-05 00:00:00

Use with the time module , documentation here

>>>import time
>>>a = u'11/5/2003'
>>>time1 = time.strptime(a, "%m/%d/%Y")
>>>print time1
time.struct_time(tm_year=2003, tm_mon=11, tm_mday=5, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=309, tm_isdst=-1)

Thanks to the comment reply from @David Cain:

The dateutil library can parse datetime strings (inferring their format). However, note that "11/5/2003" is not an unambiguous format (MM/DD or DD/MM differs by locale), so dateutil should be used with caution in this case. – David Cain

So an alternative good practice is to use the dateutil library:

>>> from dateutil.parser import parse
>>> dt = parse('2016/12/05 05:18 pm')
>>> dt
datetime.datetime(2016, 12, 5, 17, 18)
>>> dt.timestamp()
1480929480.0

>>> parse('16/11/12')
>>> datetime.datetime(2012, 11, 16, 0, 0)

strtotime you use to work with in PHP may be most similar to dateparser .

Note if you don't have the dateparser library.

Install it with

pip3 install dateparser 

If you are using conda

conda install dateparser

This library understands over 200 language locales plus numerous formats in a language agnostic fashion.

Parsing generic terms also works:

import dateparser
print (dateparser.parse('yesterday')) #2019-05-19 08:08:14.934992

And also supports non-Gregorian calendar systems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM