How to Convert a String into a Datetime Object in Python 


               


How to Convert a String into a Datetime Object in Python



Python


In this article, we show how to convert a string into a datetime object in Python.

Many times a date may simply be written as a string in Python.

However, many times, we want to convert this string object into a datetime object.

There are many advantages of converting a string to a datetime object in Python. As a datetime object, we can easily extract different elements of the datetime object, such as the year, month, day, hour, minute, and second. As a datetime object, there are year, month, day, hour, minute, and second attributes, which allow us to extract data more easily than if we kept the original string object. Also with a datetime object, we can take advantage of the timedelta class, which allows us find the difference in tie between 2 dates.

So how do we convert strings into datetime objects in Python?

We can do this though the strptime() function.

Inside of this strptime() function, we specify the string object that we want to convert as the first parameter and the format of the date that the string object is in.

We set this value equal to a variable; this variable is then the converted datetime object of the string object.

In the following code below, we convert the string, "06/15/1987" to a datetime object in Python.



So let's now go over this code.

So the first thing we have to do is import the datetime module. The datetime module allows us to create a datetime object in Python using the datetime class in the datetime module. The datetime class allow us to create datetime objects. We create an instance of the datetime class (which is a datetime object). >>> import datetime >>> birthday= "06/15/1987" >>> type(birthday) >>> birthday_dateobject= datetime.datetime.strptime(birthday, "%m/%d/%Y") >>> print(birthday_dateobject) 1987-06-15 00:00:00 >>> type(birthday_dateobject) >>> birthday_dateobject.year 1987 >>> birthday_dateobject.month 6 >>> birthday_dateobject.day 15

We then create a variable, birthday. We set this equal to, "06/15/1987"

This string represents June 15, 1987.

Just to prove that this is a string, we use the type() function to get the type of object it is. Python returns the class of string. So this definitively proves that this is a string and not a date object.

We then create another variable, birthday_dateobject, and set this equal to, datetime.datetime.strptime(birthday, "%m/%d/%Y")

What this line does is it creates a datetime object of the datetime module and converts the birthday variable to a datetime object. We must specify the format the string object is in. Being that the month and day are separated by a forward slash (/), we put a forward slash in between the month and day. We do the same thing in between the day and year.

%m references the month.

%d references the day.

And %Y references the 4-digit year.

Therefore, we now have the string converted to a datetime object in Python.

We can now use all the standard datetime object attributes to get things such as the year, month, day, hour, minute, and second of a datetime object.

We then do things such as take these times and store them in a datebase if we want.

We will run one more example now showing a datetime object that has a date and a time.

This is shown in the following code below.

So now we have a string object that has a date and a time. This is stored in the date1 variable.

We then create another variable, date1_object, and set it equal to, datetime.datetime.strptime(date1, "%m/%d/%Y %I:%M:%S")

Again, this code exists so that we can covert a string into a datetime ojbect. In order to do this, we must pass in the string, which is date1, as the first parameter. Then we must specify what is what in the string. We must specify the format. To do this, we must specify the complete format of the string. We do this with Python date format identifiers.

%m represents the month.

%d represents the day.

%Y represents the 4-digit year.

%I represents the 12-hour hour format.

%M represents the minute.

%S represents the second.

Once this datetime object is now created, we can get all the attributes, just like before, such as the year, month, day, hour, minute, and second.

Below is a full table representing the different format identifiers for date and time objects in Python.

Directive Meaning
%a Weekday's abbreviated name
%A Weekday's full name
%w Weekday as a decimal number; 0= Sunday and 6=Saturday
%d Day of the month as a zero-padded decimal number
%b Month's abbreviated name
%B Month's full name
%m Month as a zero-padded decimal number
%y Year without centry as a zero-padded decimal number
%Y Year with century as a decimal number
%H Hour (24-hour time) as a zero-padded decimal number
%I Hour (12-hour time) as a zero-padded decimal number
%p Locale's equivalent of either AM or PM
%M Minute as a zero-padded decimal number
%S Second as a zero-padded decimal number
%f Microsecond as a decimal number, zero-padded on the left
%z UTC offset in the form +HHMM or -HHMM
%Z Time zone name (empty string if the object is naive)
$j Day of the year as a zero-padded decimal number


So we can use all these identifiers in Python in order to convert a string into a datetime object.

And this is how we can convert a string into a datetime object in Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...