Categories
datetime python

Converting string “Jun 1 2005 1:33PM” into datetime

2835

How do I convert the following string to a datetime object?

"Jun 1 2005  1:33PM"

4

  • 10

    Unless you’re sure one format handles every single date-time (no ”, no NaNs, no incompletes, no format mismatches, no trailing characters, timezones, microsecond timestamps, or other text…), the exception-happiness of strptime() will drive you nuts, unless you wrap it. See my answer, based on Or Weis answer to this

    – smci

    Dec 15, 2017 at 3:00


  • The laziest, most widely usable approach I know is dateparser (check blog.scrapinghub.com/2015/11/09/…). It works even with natural language time expressions in several languages out of the box. I guess it can be slow though.

    – Armando

    Nov 1, 2019 at 17:23

  • There is a helpful link here: stackabuse.com/converting-strings-to-datetime-in-python

    Jan 4, 2020 at 14:37

  • 1

    datetime.strptime as others have mentioned. For those who prefer a video explanation, see here.

    – Ben

    Feb 3, 2021 at 5:03


4324

datetime.strptime is the main routine for parsing strings into datetimes. It can handle all sorts of formats, with the format determined by a format string you give it:

from datetime import datetime

datetime_object = datetime.strptime('Jun 1 2005  1:33PM', '%b %d %Y %I:%M%p')

The resulting datetime object is timezone-naive.

Links:

Notes:

  • strptime = “string parse time”
  • strftime = “string format time”
  • Pronounce it out loud today & you won’t have to search for it again in 6 months.

Also, as seen in a comment made by @Izkata, if you want a date instead of a datetime, going through datetime handles it nicely:

datetime.strptime('Jun 1 2005', '%b %d %Y').date() == date(2005, 6, 1)

5

  • 9

    ‘%b’, ‘%p’ may fail in non-English locale.

    – jfs

    Apr 29, 2014 at 10:55

  • 1

    What is the string doesn’t have the time, just “April 25, 2014”

    – User

    Apr 30, 2014 at 1:56

  • 33

    @User You’ll have to know ahead of time to exclude that part of the format string, but if you want a date instead of a datetime, going through datetime handles it nicely: datetime.strptime('Jun 1 2005', '%b %d %Y').date() == date(2005, 6, 1)

    – Izkata

    Nov 11, 2014 at 20:02

  • 19

    If you know the string represents a datetime in UTC, you can get a timezone aware datetime object by adding this line in Python 3: from datetime import timezone; datetime_object = datetime_object.replace(tzinfo=timezone.utc)

    – Flimm

    Dec 8, 2016 at 10:28

  • 1

    In my case, “stackoverflow.com/a/54830426/6784445” answer was a better match and I was hoping if we could include it to this solution as a complement.

    – hm6

    Feb 4 at 21:30


1032

Use the third party dateutil library:

from dateutil import parser
parser.parse("Aug 28 1999 12:00AM")  # datetime.datetime(1999, 8, 28, 0, 0)

It can handle most date formats, including the one you need to parse. It’s more convenient than strptime as it can guess the correct format most of the time.

It’s very useful for writing tests, where readability is more important than performance.

You can install it with:

pip install python-dateutil

4

  • 121

    Be aware that for large data amounts this might not be the most optimal way to approach the problem. Guessing the format every single time may be horribly slow.

    Jul 3, 2011 at 0:08

  • 25

    This is nice but it would be nice to have a solution that is built-in rather than having to go to a third party.

    Oct 12, 2011 at 20:33

  • 2

    This is great for situations where you cannot guarantee the date format.

    – wisbucky

    Nov 14, 2021 at 6:16

  • If you have uncertain formats and some of those are incomplete, like June 2009 instead of 12 June 2009, it would assume any arbitrary day. Same goes for dates without month.

    Mar 2 at 10:06

512

Check out strptime in the time module. It is the inverse of strftime.

$ python
>>> import time
>>> my_time = time.strptime('Jun 1 2005  1:33PM', '%b %d %Y %I:%M%p')
time.struct_time(tm_year=2005, tm_mon=6, tm_mday=1,
                 tm_hour=13, tm_min=33, tm_sec=0,
                 tm_wday=2, tm_yday=152, tm_isdst=-1)

timestamp = time.mktime(my_time)
# convert time object to datetime
from datetime import datetime
my_datetime = datetime.fromtimestamp(timestamp)
# convert time object to date
from datetime import date
my_date = date.fromtimestamp(timestamp)

1

  • 18

    From what I understand, this answer only outputs time objects, not datetime objects — which is why the answer would be buried compared to Patrick’s answer.

    Sep 7, 2010 at 13:08