How to convert a string to a DateTime object in Python?


In Python, there are a number of ways to convert a string to a DateTime object. One popular way is to use the strptime() method. The strptime() method is a powerful tool that allows you to specify the format of the string as well as how you want the string to be converted into a DateTime object.

How .strptime() Works

Use the strptime() method in Python to convert a string to a datetime.

.strptime() is a built-in method of the datetime class that is used to turn a string representation of the date/time into a datetime object.

The .strptime() method takes two arguments:

  • 1) The string that you want to convert
  • 2) The format of the string

The strptime() method returns a DateTime object. If the string cannot be parsed according to the format, then an error is raised

The table below shows the format codes that you can pass to the .strptime() method.

  • %Y — Representing the Year and it ranges from 0001 to 9999
  • %m — Representing the month of a year and it ranges from 01 to 12.
  • %d — Representing the days of the month and ranges from 01 to 31.
  • %H — Representing the hours of the day in a 24-hour format and ranges from 00 to 23.
  • %I — Representing the hours of the day in a 12-hour format and ranges from 01 to 12.
  • %M — Representing minutes in an hour and ranges from 00 to 59.
  • %S — Representing the seconds in a minute and ranges from 00 to 59 as well.
  • %p — AM/PM
  • %b — Month name, short version
  • %B — Month name, full version
  • %a — Weekday, short version
  • %A — Weekday, full version

Example 1: Using .strptime() Method

Let’s say you have a string “12/06/2023”. You can use the strptime() method to parse this string into a DateTime object. The first argument is the string and the second argument is the format.

In this case, we want to convert the string into a datetime object with the format “%d/%m/%Y %H:%M:%S”.

Here’s what that would look like:

from datetime import datetime # Date String in dd/mm/yyyy HH:MM:SS format datetime_string = "12/06/2023 09:15:32" # Convert string to datetime object datetime_object = datetime.strptime(datetime_string, "%d/%m/%Y %H:%M:%S") print(datetime_object) # Output 2023-06-12 09:15:32
Code language: Python (python)

Output:

2023-06-12 09:15:32
Code language: CSS (css)

Example 2: To convert DateTime to string using time.strftime

import time # Function to convert string to datetime def convert(str_datetime): str_datetime = time.mktime(str_datetime) format = "%b %d %Y %r" # The format dateTime = time.strftime(format, time.gmtime(str_datetime)) return dateTime # Driver code date_time = (2023, 12, 4, 10, 7, 00, 1, 48, 0) print(convert(date_time))
Code language: Python (python)

Output:

Dec 04 2023 03:07:00 AM
Code language: CSS (css)

Example 3: string-to-date conversion

from datetime import datetime date_str = "2023/08/09" format = "%Y/%m/%d" #specifify the format of the date_string. mydate = datetime.strptime(date_str, format) print(mydate)
Code language: Python (python)

Output

2023-08-09 00:00:00
Code language: CSS (css)

Example 4:

There are three types of objects in the datetime module: date, time, and datetime. The date object has the date, the time object has the time, and the datetime object has both the date and the time.

import datetime datetime_str = '2023-06-29 09:16:25.243861' datetime_obj = datetime.datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S.%f') print('Date:', datetime_obj.date()) print('Time:', datetime_obj.time()) print('Date and time:', datetime_obj)
Code language: Python (python)

Output

# Date: 2023-06-29 # Time: 09:16:25.243861 # Date and time: 2023-06-29 09:16:25.243861
Code language: Python (python)

Example 5:

import datetime datetime_str = 'Jun 27 2023 7:41AM' datetime_obj = datetime.datetime.strptime(datetime_str, '%b %d %Y %I:%M%p') print('Date:', datetime_obj.date()) print('Time:', datetime_obj.time()) print('Date-time:', datetime_obj)
Code language: Python (python)

Output

Date: 2023-06-27 Time: 07:41:00 Date-time: 2023-06-27 07:41:00
Code language: Python (python)

Here are some more common time formats and the tokens that are used to parse them:

"Jun 28 2023 at 7:41AM" -> "%b %d %Y at %I:%M%p" "September 23, 2023, 22:19:56" -> "%B %d, %Y, %H:%M:%S" "Sun,05/12/98,12:31PM" -> "%a,%d/%m/%y,%I:%M%p" "Mon, 21 March, 2023" -> "%a, %d %B, %Y" "2023-03-12T11:12:44Z" -> "%Y-%m-%dT%H:%M:%SZ"
Code language: JavaScript (javascript)

Example 6: This would be helpful for converting a string to DateTime and also with a time zone:

from datetime import datetime import pytz def convert_string_to_time(date_string, timezone): datetime_obj = datetime.strptime(date_string[:26], '%Y-%m-%d %H:%M:%S.%f') datetime_obj_timezone = pytz.timezone(timezone).localize(datetime_obj) return datetime_obj_timezone date = '2023-08-14 13:09:24.543953+00:00' TIME_ZONE = 'UTC' datetime_obj_timezone = convert_string_to_time(date, TIME_ZONE) print(datetime_obj_timezone)
Code language: Python (python)

Output:

2023-08-14 13:09:24.543953+00:00
Code language: CSS (css)

Example 7: using dateparser module

pip install dateparser
import dateparser mydatetime1 = 'Jun 1 2023 1:33PM' mydatetime2 = 'Aug 28 2025 12:00AM' datetime_obj1 = dateparser.parse(mydatetime1) datetime_obj2 = dateparser.parse(mydatetime2) print(datetime_obj1) print(datetime_obj2)
Code language: Python (python)

Output:

2023-06-01 13:33:00 2025-08-28 00:00:00
Code language: Python (python)

Example 8: Python Convert String to Datetime with locale

Let’s look at an example of how a string that depends on the locale will be turned into a datetime object. We will use the locale module to set the locale that Python will use.

import locale locale.setlocale(locale.LC_ALL, 'de_DE') datestr_de_DE = '10-Dezember-2023 Montag' # de_DE locale datetime_obj = datetime.strptime(datestr_de_DE, '%d-%B-%Y %A') print(datetime_obj)
Code language: Python (python)

Output

2023-12-10 00:00:00
Code language: CSS (css)

We hope you found this post helpful. Happy coding!

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts