Converting strings which contains dates and/or time is easy in Python using datetime and time modules. Even bad formatted date formats can be converted to datetime objects.

In this post:

  • Python convert string(with date) to datetime
  • Python convert string(with date) to datetime no time
  • Python convert string(with date) to pure date
  • Python convert string(with date) to time
  • Python split date string to parts with module time
  • Python extract dates from text and convert to datetime
  • References

Python convert string(with date) to datetime

The usual date format can be converted with method strptime from datetime module:

simpleDate = datetime.strptime('May 2 2018  11:23PM', '%b %d %Y %I:%M%p')
print(simpleDate)

result:

2018-05-02 23:23:00

In case of wrong format or input you will get error ValueError:

simpleDate = datetime.strptime('May 2 2018', '%b %d %Y %I:%M%p')
print(simpleDate)

result:

ValueError: time data 'May 2 2018' does not match format '%b %d %Y %I:%M%p'

Python convert string(with date) to datetime no time

If your string contains date only you can convert it to datetime with default time 00:00:00 :

simpleDate = datetime.strptime('May 2 2018', '%b %d %Y')
print(simpleDate)

result:

2018-05-02 00:00:00

Python convert string(with date) to pure date

Converting string to date can be done in two steps:

  • convert the string to datetime with strptime()
  • converrt the datetime to date
dateOnly = datetime.strptime('May 2 2018', '%b %d %Y').date()
print(dateOnly)

result:

2018-05-02

Python convert string(with date) to time

The same way we can convert string datetime to time only:

simpleTime = datetime.strptime('11:23PM', '%I:%M%p').time()
print(simpleTime)

result:

23:23:00

Python split date string to parts with module time

Let say that you have dates stored as strings in many different formats. And you want to get the atomic parts of these dates like: year, month, day, hour, minutes. In this case you can use module time:

dateParts = time.strptime('May 2 2018  11:23PM', '%b %d %Y %I:%M%p')

print(dateParts)
print(dateParts.tm_year)
print(dateParts.tm_mon)
print(dateParts.tm_mday)

result:

time.struct_time(tm_year=2018, tm_mon=5, tm_mday=2, tm_hour=23, tm_min=23, tm_sec=0, tm_wday=2, tm_yday=122, tm_isdst=-1)
2018
5
2

In case of wrong input or format you will get error:

dateParts = time.strptime('May 2 2018  11:23PM', '%b %d %Y ')

result:

ValueError: unconverted data remains: 11:23PM

Python extract dates from text and convert to datetime

You can parse your text or string and match all dates by using regular expression:

import re

str = """COBOL is a compiled English-like computer programming language designed for business use. 122. On 10/10/2015 is a big date unlike 1/11/2010 """

all = re.findall(r"[\d]{1,2}/[\d]{1,2}/[\d]{4}", str) # match capital letters

for s in all:
    print(s)

result:

10/10/2015
1/11/2010

More examples here: python regex matches dates from string

References

If you want to have a look on the official documentation for datetime and time you can check them here: