Menu Close

Python Time Module Tutorial

In this article, we are going to learn all about the Python time module along with examples. In the previous tutorial, we have seen all about the Python DateTime module with the help of various examples.

In this guide, you will see almost all the attributes and methods of the python time module which are very helpful to work with time.

To understand this article, you should have basic knowledge of Python programming.

Python time module

time module is a python built-in module that is used to work with time. we will learn different time-related functions which are defined in the time module using an appropriate example.

To use of time module you need to import time module using import keyword.

Here we will see time module’s functions that are used to make easy to work with time.

Python time.time()

The time() function return the number of seconds passed since epoch.

import time
seconds = time.time()
print('Total seconds:- ', seconds)

Python time.ctime()

The ctime() function take seconds passed since epoch and convert into the string local time.

import time
seconds = 1545925769.9618232
local_time = time.ctime(seconds)

Python time.sleep()

sleep() method delay the current execution of thread to a given number of seconds.

import time
print("Hello, Welcome to ")
print("Programming Funda")

Python time.strftime()

Python strftime() method used to get the formatted time string.

import time
result = time.strftime('%Y-%m-%d %H:%M:%S')

Python time.strptime()

Python time strptime() method parses the string representing time according to the format. The return value is struct_time.

import time
string_date = "30 Jun 21"
parsed_date = time.strptime(string_date, "%d %b %y")

class time.struct_time

It is the type of the time value sequence returned by the gmtime(), localtime() and strptime().it is the object of a named tuple. Value can be access through index number and by attribute name.

You can find out the index number and attribute name to access the value.

IndexAttribute NameValue
6tm_wdayrange[0,6], 0 is Monday
8tm_isdst0,1 or -1
import time
string_date = "30 Jun 21"
parsed_date = time.strptime(string_date, "%d %b %y")

#Output time.struct_time(tm_year=2021, tm_mon=6, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=181, tm_isdst=-1)

Python time.time_ns()

This method is used to return the time as the integer number of nanoseconds.

import time

Python time.localtime()

Python time module localtime() method return the structured time tuple.

import time
result = time.localtime()

Python time.gmtime()

Converts a timestamp to the structured time of the UTC time zone.

import time
seconds = time.time()
result = time.gmtime(seconds)

Python time.asctime()

asctime() method is used to convert structured time to string time format for example ‘Thu Feb 20 16:51:03 2020‘. The default value for parameter t is time.localtime().

import time
result = time.localtime()

Python time.mktime()

The mktime() function takes struct_time( or a tuple containing 9 elements corresponding to the struct_time) as an argument and return the seconds passed in an epoch in local time.

import time
struct_time = time.localtime()
s = time.mktime(struct_time)


import time
t = (2021, 5, 5, 16, 54, 20, 2, 125, 0)
s = time.mktime(t)


In this article, you have learned all about the Python time module along with examples. When you want to work with time in your Python application, Then the module is going to best for you.

I hope this article very helpful for you, If you like this Python time module tutorial, don’t forget to share.

Reference:- Click Here

Python Datetime Module Tutorial
Python HTML Module Tutorial

Related Posts