Convert String to datetime Python w3schools Tutorial Guide

Logan Jul 13, 2026

When working with dates and times in Python, especially when dealing with data extracted from sources like databases or external APIs, you often encounter strings that represent dates. To perform any date-related operations, you first need to convert these strings into Python's datetime objects. Let's delve into how you can accomplish this, using the datetime module, which is a part of Python's standard library.

a computer screen with the words python string method
a computer screen with the words python string method

The datetime module provides the datetime function to convert a date string into a datetime object. However, the function is quite flexible, accepting various date string formats. If the input string format doesn't match any of the formats datetime can understand by default, you may need to provide a formatting directive using the strftime function.

How to Lowercase Python String with Example Programs
How to Lowercase Python String with Example Programs

Understanding Date String Formatting

It's crucial to understand the date string formats that Python's datetime can interpret without any additional formatting. These formats include 'YYYY-MM-DD', 'DD-MM-YYYY', 'MM-DD-YYYY', etc. Let's explore these formats.

Master Python Strings: Essential Concepts Every Beginner Should Learn
Master Python Strings: Essential Concepts Every Beginner Should Learn

Year-Month-Day Format

The most common date string format is year-month-day, typically represented as 'YYYY-MM-DD'. For instance, '2022-01-01' represents January 1, 2022.

Python String Methods Cheat Sheet for Beginners | Learn upper(), lower(), split(), replace() & More
Python String Methods Cheat Sheet for Beginners | Learn upper(), lower(), split(), replace() & More

Python's datetime understands this format by default. Here's an example:

```python from datetime import datetime date_str = '2022-01-01' date_obj = datetime.strptime(date_str, '%Y-%m-%d') ```

Day-Month-Year Format

In some regions, the day is listed before the month, formats like 'DD-MM-YYYY' are used. For example, '01-01-2022' also represents January 1, 2022.

a poster with the words string method in python
a poster with the words string method in python

Python understands this format too. Here's how to convert such a string into a datetime object:

```python from datetime import datetime date_str = '01-01-2022' date_obj = datetime.strptime(date_str, '%d-%m-%Y') ```

Using strftime to Specify Custom Formats

If your date strings don't match any of the formats Python datetime can understand by default, you can use the strftime function to specify your own format. Let's suppose your date strings are in the format 'DD Mon YYYY', like '01 Jan 2022'.

Python String Methods
Python String Methods

Specifying Custom Formats

The strftime function takes two arguments: the first argument is the date string and the second argument is the format you want to convert it into. In this case, you want to convert it into a datetime object. Here's how you do it:

5 Python Tricks That Will Save You Hours of Coding
5 Python Tricks That Will Save You Hours of Coding
15 Essential Python String Functions Every Beginner Must Know (Python Cheat Sheet)
15 Essential Python String Functions Every Beginner Must Know (Python Cheat Sheet)
Python Tutorial
Python Tutorial
Python Projects with Source Code (Beginner to Advanced)
Python Projects with Source Code (Beginner to Advanced)
python
python
🔠 Python String Methods: Master String Manipulation 🔠
🔠 Python String Methods: Master String Manipulation 🔠
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
7 Cool Python Projects for Kids to Try!
7 Cool Python Projects for Kids to Try!
💜 Save this Python String Methods Notes for later!
💜 Save this Python String Methods Notes for later!
25 Python Project Ideas for Beginners (With Tech Stack)
25 Python Project Ideas for Beginners (With Tech Stack)
Python Program to output Calendar for a month - EasyCodeBook.com
Python Program to output Calendar for a month - EasyCodeBook.com
String Conversion in Python: When to Use __repr__ vs __str__
String Conversion in Python: When to Use __repr__ vs __str__
Introduction to Time Series with Python
Introduction to Time Series with Python
a man holding up a poster with the words 5 levels of python on it
a man holding up a poster with the words 5 levels of python on it
10 Fun Python Exercises for Kids
10 Fun Python Exercises for Kids
Learn Python in 10 days
Learn Python in 10 days
three different types of python programming
three different types of python programming
Python Projects for Beginners 🐍
Python Projects for Beginners 🐍

```python from datetime import datetime date_str = '01 Jan 2022' date_obj = datetime.strptime(date_str, '%d %b %Y') ```

Python's datetime module provides a powerful and flexible way to handle dates and times in your Python scripts. Understanding how to convert date strings into datetime objects is an essential aspect of working with dates in Python.

Whether you're processing dates from a database, a web API, or even user input, the datetime module provides the tools you need to manage dates and times in your Python applications. Thanks for reading, and happy coding!