--- title: "How to Convert a String to Datetime in Python?" date: "2022-12-14" categories: - "python" coverImage: "calendar.png" --- # How to Convert a String to Datetime in Python? Working with dates and times can be tricky, but Python makes it manageable. The dates and times can be represented in various forms, and one of the most convenient is strings. However, we need to convert this string data into DateTime objects to work with these dates and times for arithmetic manipulations like finding time differences, adding or subtracting time, and more. This tutorial will look at converting a string date and time to a DateTime object in Python using the Datetime module. In addition, we'll also understand the behavior of date and time in different time zones and learn about different output types of date and time objects. ## What is Python Datetime? The primary method we will use is the [DateTime](https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior "DateTime Module Documentation") module. Datetime is a built-in Python module that provides functions to handle many complex functions related to date and time. The DateTime module has three basic types of objects: date, time, and datetime. Date works with dates, time works with time, and datetime works with both dates and times. ## Converting Strings to Datetime Objects Python strptime() is a built-in method of the DateTime class that converts a string of date and time into DateTime objects. Its syntax is as follows: ```python datetime.strptime(date_string, format) ``` ![](/images/how-to-convert-a-string-to-datetime-in-python/strptime-1024x465.png "strptime()") Here both arguments are required and must be strings. **date\_string** \- the date (can be time as well) represented in string format. **format** \- the data represented in datetime format, which is converted from date\_string with this function. ![](/images/how-to-convert-a-string-to-datetime-in-python/DateTime-Module-1024x558.png) If no format string is specified, it defaults to "%a %b %d %H:%M:%S %Y." If the input string does not match the provided format, the ValueError occurs. ## Python Datetime Format Codes Before converting the date and time string to DateTime, let's look at a fundamental concept: formatting codes. Dates have a default representation - DD/MM/YYYY, for example, but we can output them in a particular format using different format codes. Each formatting code represents a different part of the date/time, such as day, month, year, day of the month or week, and so on. Below is a list of date and time formatting codes:
Directive | Meaning | Returns |
%a | Abbreviated names of weekdays | Sun, Mon, ... , Sat |
%A | Full names of weekdays | Sunday, Monday, ... , Saturday |
%w | Weekday as a number, where 0 is Sunday, and 6 is Saturday | 0, 1, 2, ... , 6 |
%d | Days of months as zero-padded numbers | 01, 02, 03, ... , 31 |
%b | Abbreviated names of months | Jan, Feb, ... , Dec |
%B | Full names of months | January, February, ... , December |
%m | Months as zero-padded numbers | 01, 02, 03, ... , 12 |
%y | Years without century as zero-padded numbers | 00, 01, 02, 03, ... , 99 |
%Y | Years with centuries as zero-padded numbers | 0001, 0002, 0003, ... , 2019, 2020, ... , 9998, 9999 |
%H | Hours (24 hours) as zero-padded numbers | 00, 01, 03, ... , 23 |
%I | Hours (12 hours) as zero-padded numbers | 01, 02, 03, ... , 12 |
%p | The local equivalent of AM/PM | AM, PM |
%M | Minutes as zero-padded numbers | 00, 01, 02, 03, ... , 59 |
%S | Seconds as zero-padded numbers | 00, 01, 02, 03, ... , 59 |
%f | Microseconds as zero-padded to 6 digits numbers | 000000, 000001, 000002, …, 999999 |
%z | UTC offset in the form ±HHMM[SS[.ffffff]] | +0000, -0400, +1030, +063415, -030712.345216 |
%Z | Time zone name | UTC, GMT |
%j | Day of the year as zero-padded numbers | 001, 002, 003, ... , 365, 366 |
%U | Week number of the year as zero-padded numbers, where Sunday is the first day of the week | 00, 01, 03, 04, ... , 53 |
%W | Week number of the year as zero-padded numbers, where Monday is the first day of the week | 00, 01, 03, 04, ... , 53 |
%c | Date and time representation | Mon Jun 14 21:30:00 2002 |
%x | Date representation | 01/14/02 01/14/2002 14.01.2002 |
%X | Time representation | 21:30:00 |
%% | The literal '%' symbol | % |