How to convert a Python date string to a date object?



In this article, we convert a date string to a date object. We will use the strptime() method to convert a date string to a date object, and we will also use the dateutil module for flexible date parsing.

A date string is a date written in text form, like "09 May 2025" or "2025-05-09". To work with dates in Python, we will have to convert these strings into date objects. To achieve this, we can use Python's datetime and dateutil modules.

Using the strptime() Function

The strptime() method takes the date as input and converts it into a date object. Note that you cannot convert any string into a datetime object. The string should be in a certain format.

Syntax

The syntax of the strptime() method is shown in this section.

datetime.strptime(time_data, format_data)

Where,

  • time_date: It is the time present in string format.

  • format_date: It is the data(string) present in a datetime format, which is converted from time_data using this function.

Example 1

In the following example, we convert a string date to a datetime object by using the strptime. We also extract the microsecond, minute, and second.

# Import the datetime module
from datetime import datetime

# The date string
time_data = "23/01/01 04:35:7.123"

# The format of the date string
format_data = "%d/%m/%y %H:%M:%S.%f"

# Convert the date string to a datetime object
date = datetime.strptime(time_data, format_data)

# Print the date object
print("Microsecond:",date.microsecond)
print("Hour:",date.hour)
print("Minute:",date.minute)
print("Second:",date.second)
print("The date is:",date)

When you run the program, it will show this output -

Microsecond: 123000
Hour: 4
Minute: 35
Second: 7
The date is: 2001-01-23 04:35:07.123000

Example 2

In this example, we use the strptime() method to get the needed format and date() to retrieve the date from DateTime.

import datetime
# The date - 09 May 2025

# The date string
date_str = '09052025'

# The format of the date string 
format_str = '%d%m%Y' 

# Convert the date string to a datetime object
datetime_obj = datetime.datetime.strptime(date_str, format_str)

# Print the date object
print(datetime_obj.date())

After running the program, you will get this result -

2017-12-29

Example 3

In this example, we will convert a date string to a date object using the strptime() method. The input date string is provided in the format of YYYY-MM-DD.

import datetime

# Date string with full month name
date_str = 'May 9, 2025'

# Format with full month name
format_str = '%B %d, %Y'

# Convert to datetime object
datetime_obj = datetime.datetime.strptime(date_str, format_str)

# Output the date part
print(datetime_obj.date())

This output will be displayed when the program runs -

2025-05-09

Using the dateutil Module

The dateutil module is a third-party library that provides powerful extensions to the standard datetime module. It can parse dates in various formats without needing to specify the format string precisely.

Example

Now, in this program, we have used the dateutil library's parser.parse() for date parsing without giving the exact format.

from dateutil import parser

# Date string in various formats
date_str = 'May 10, 2025'

# Parse the date string into a datetime object
datetime_obj = parser.parse(date_str)

# Extract and print the date part
print(datetime_obj.date())

You will see this result after executing the program -

2025-05-10
Updated on: 2025-06-16T12:02:41+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements