How to use Python DateTime – with example

  • Reading time:18 mins read

What is Python DateTime?

Now, working with the date and the time in programming can be quite difficult, depending on time zones and various other things. But fortunately, we have a built-in Python DateTime module that we can import. That makes it a lot easier for us.

In this article, we are going to explore how to create a DateTime object and we’re going to manipulate some dates. In addition, we will learn how to turn DateTime objects into string format and then back from a string into a DateTime object so that we can work with them in our code.

 

 

Importing the Python DateTime module

The first thing that we want to do is we need to import the Python DateTime module into the working space. So to accomplish that we need to type from DateTime import DateTime. Now, this might look a little bit odd, but what we’re actually doing is that we are importing the DateTime class from the DateTime module. They both have the same name.

 

Learn Python DateTime online

 

We can now proceed to create the date object which we can then use. We should begin by creating a variable that will store this event and then we will equate that to datetime.now(). In this example, we will name the variable “now”, so that if we now proceed to print the variable out we will be able to obtain the present day, year and month.

 

 

Alternatively, if we just want the date from the Python DateTime, we can actually just import the date and instead of datetime.now() we could have date.today()

Now, this is going to give us the same result, however, it’s not going to have the time attached to it. So depending on what you actually want to use this form if you don’t need the time, you might want to use date.today().

 

 

But going forward in this example, we will be using the full datetime.now(), which gives us access to everything

Turning Python DateTime objects to string

Now, in a more realistic use case, we might find that we need to turn this object into an actual string, so we can put it in other places in our code into files.  Now to do that, we use strftime(), which basically lets us turn the Python DateTime objects into a string.

Example

So to accomplish that we need to assign, date to be equal to now.strftime(), note that we are using now because we’re referencing the daytime object that we created.

 This is going to be the method that turns the Python DateTime objects into a string.

 

 

However,  we need to tell it the format of the output that we want. Therefore we need to use some formatting codes with the parentheses. The are several formats that one can choose from, for instance, we can use one of the common ones that start with the day then the month and finally the year

Now to do that, we need to insert the quote marks because this is now a string and then a % sign followed by a letter sorry to represent that part of the day object. Now, a small “%d” represents the date. So if we proceed to print the date we will obtain the current date as shown below.

 

 

We will now add the day, month, year format proposed above. To do that we need to append a slash, a % sign and an ‘m’. In this case, a small m will give us the month number format and then another slash and then the % sign followed by a small y which will give us the last two digits of the year.

 

 

If we opt to use a capital Y instead of a small y in our code, We will get a year in full format i.e “11 6 2021”.

 

 

In one of the formats, we could also use capital B instead of the smallcase ‘m’. This format returns the name of the month instead of just the number.

Turning date from string format to Python DateTime object

We can take a date given in string format and we can turn that into a Python DateTime object. We basically do what we’ve done above, but this time in reverse.Instead of using strftime(), we’re going to use strptime().

Example

If we say date_given = “11 June 21”, now this could possibly have come from an excel file or maybe a CSV file that you’re given and you want to be able to import that into your code and turn it into an actual date object that we can then manipulate.

Now to do that we will begin by creating a new variable that we are going to call date and then equate that to datetime.strptime(). We can now pass in the variable date_given so that the object knows what it is taking.

 

 

And now we need to match the format, just like we did in the previous example.

In this case, we are going to use “%d‘ to represent the day and “%B” for the month in full and finally a small “%y” for the year in short format. If we now print the date out again we will get the Python DateTime object returned in ISO format, which is going to be the format when dealing with computers and date objects.

 

 

So if we change date_given to  “12th Of June”, we will get 12th June as the output. The output in this case does have a time object at the end, but we can remove that if we add a dot date at the end. As shown below.

 

 

Summary

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.