How to use the Python Time module – with examples

  • Reading time:17 mins read

What is the Python Time module used for?

The Python time module provides a variety of functions that are all related to time. In order to use the functions defined under the Python time module we first need to import them into our current workspace.

Since the module exists in the core Python we do not need to install anything we just need to import the module at the top of our code.

 

How you can use the Python time module?

Using the Python time.time() method we actually get the floating-point value of the number of seconds that have passed since the epoch, which is the beginning of time.

As far as computers are concerned, the beginning of time was January of 1970. So if we print the output of the time.time() function we are going to get a 16 long number of seconds that have passed since the epoch.

 

 

Although this is useful, this is not actually what we are after. Fortunately, we can change this by calling different parts of the Python time module.

Suppose we need the Python time represented in the form of a string instead of a floating-point. Python provides the ctime() function that allows us to do just that.

This function returns the current time Month. Day and year in the form of a string. (Please note that this time will vary depending on the geographical location as well as the time when you run the code below).

 

 

This is much easier to understand than the initial representation and more readable to humans.  However, this may not be the desired format, or perhaps we may want the time or the date represented as opposed to getting the whole string.

In this case, we are going to use the strftime(), which is the string format time. This function allows us to use different standard codes to represent time and date. Using these codes we can customize the time, year and month formats. 

 To get started with this function we first need to create a variable that we will equate to the function as shown here: time_now = time.strftime(), we can now go ahead and pass in the arguments. 

 

python time

 

Using the codes provided by the function we can tweak the function to give us time with no seconds or just hours and minutes just like in the case of the digital hand clock.

We are going to use two codes; %H and %M that represent hours and minutes respectively. Finally, we need to pass in the argument time.localtime() which returns the current time depending on your location in the world.

If we print out the time_now variable this is going to give us the hours and minutes that we asked for within the time object.

 

 

We can further customize this, for instance, we can also add in the code that represents the seconds and if we rerun the code again we are going to the seconds as well.

 

 

We can also make a fully customized time string from the Python time module. In the case above we have the codes capital H, M and S representing hours, minutes and seconds respectively.

 

python time

 

We can also represent the month, year and date using similar codes. Using the lower case letters d and m to represent day and month respectively and uppercase Y to represent the year. 

 

 

Using uppercase Y as the code representing the year lets us get 2021 as the year, on the other hand using lowercase y will give us the short form of the year.

 

 

In a similar manner, we can also the locale’s abbreviated month name using lowercase b which is OCT at the time of writing this article. And the Locale’s full month name using uppercase B which is October.

 

 

In most cases, we are going to need time in formats along the lines of date, month, year or something similar to that.

The Python time function offers a lot of flexibility when it comes to customizing date objects, including the ability to change separators from full colons to something else.

 

 

We can also switch to little dashes that are used quite often to also separate date month and year.

 

 

Furthermore, one of the most common important things about the Python time module is that we can store our string formatted time object and use it wherever we want in our code.

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.

Programming tutorials

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.

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.