Python String to Datetime

Python String to Datetime

You can convert a Date and time string to Datetime object in Python.

datetime package contains methods in date, time and datetime classes to convert a string into date, time or datetime objects.

In this tutorial, we will convert a string to Datetime object using following methods.

  • datetime.datetime.fromisoformat()
  • datetime.date.fromisoformat()
  • datetime.time.fromisoformat()

Examples

1. Convert date and time string to datetime object

In this example, we shall take some of the possible ISO formatted strings for date and time, and convert them to datetime.datetime objects using datetime.datetime.fromisoformat(datetimeString) method.

Python Program

from datetime import datetime

dt1 = datetime.fromisoformat('2020-11-18')
print(type(dt1), dt1)

dt2 = datetime.fromisoformat('2020-11-18T00:05:23')
print(type(dt2), dt2)

dt3 = datetime.fromisoformat('2020-11-18 00:05:23.283')
print(type(dt3), dt3)

dt4 = datetime.fromisoformat('2020-11-18 00:05:23.283+00:00')
print(type(dt4), dt4)

dt5 = datetime.fromisoformat('2020-11-18T00:05:23+04:00')   
print(type(dt5), dt5)
Run Code Copy

Output

<class 'datetime.datetime'> 2020-11-18 00:00:00
<class 'datetime.datetime'> 2020-11-18 00:05:23
<class 'datetime.datetime'> 2020-11-18 00:05:23.283000
<class 'datetime.datetime'> 2020-11-18 00:05:23.283000+00:00
<class 'datetime.datetime'> 2020-11-18 00:05:23+04:00

After you convert a string to datetime object, you can access the individual parts of the date like year, month, time, etc., using strftime().

In the following program, we shall convert a string to datetime object and print hours, day of the week.

Python Program

from datetime import datetime

dt = datetime.fromisoformat('2020-11-18 17:05:23.283')

# Print hours
print(dt.strftime("%H"))

# Print weekday
print(dt.strftime("%A"))
Run Code Copy

Output

17
Wednesday

2. Convert date string to date object

In this example, we shall convert ISO formatted Date sting to datetime.date object using datetime.date.fromisoformat(datetimeString) method.

Python Program

from datetime import date

# Convert string to date object
dateStr = '2020-11-18'
date1 = date.fromisoformat(dateStr)

# Access date object
print(date1)
print(date1.strftime('%Y')) #year
print(date1.strftime('%B')) #month name
print(date1.strftime('%d')) #day of month
Run Code Copy

Output

<class 'datetime.date'> 2020-11-18
2020
November
18

3. Convert time string to time object

In this example, we shall take some of the possible ISO formatted time strings and convert them to datetime.time objects using datetime.time.fromisoformat(datetimeString) method.

Python Program

from datetime import time

time2 = time.fromisoformat('17:15:23')
print(type(time2), time2)

time3 = time.fromisoformat('17:15:23.283')
print(type(time3), time3)

time4 = time.fromisoformat('17:15:23.283+00:00')
print(type(time4), time4)

time5 = time.fromisoformat('17:15:23+04:00')   
print(type(time5), time5)
Run Code Copy

Output

<class 'datetime.time'> 17:15:23
<class 'datetime.time'> 17:15:23.283000
<class 'datetime.time'> 17:15:23.283000+00:00
<class 'datetime.time'> 17:15:23+04:00

Summary

In this tutorial of Python Examples, we learned how to convert a string to datetime object, with the help of well detailed examples.

Related Tutorials

Code copied to clipboard successfully 👍