How to Get Time in Python

Handling time in Python is a crucial aspect of many applications. Whether you want to timestamp an event, measure runtime, create a timer, or simply display the current time, Python provides several tools making it very straightforward to work with time. In this tutorial, we will cover different ways to get the current time in Python.

Step 1: Using Time Module

Python’s time module provides various functions to manipulate time. To get the current time, we can use the time() function from the time module as in the example below:

The time() function returns the current time in seconds since the epoch as a floating point number. It does not format the date in the way humans are accustomed to reading it.

Step 2: Formatting Time using Ctime or Asctime

Current time retrieved using the time() function isn’t very readable. To get a more readable format, we can use the ctime() or asctime() functions from the time module as shown below:

Both ctime() and asctime() functions return a string representing the current time in the form: ‘Tue Apr 16 11:34:12 2021’.

Step 3: Using Datetime Module

Python’s datetime module also provides functions to work with date and time. The function returns the current date and time:

The output of will be in the format 2021-04-16 13:08:18.356941.

Full Code Block Below:


As you can see, getting the current time in Python is straightforward. Whether you want to track the execution time of your program, timestamp events in a log file, or just display the current date and time in a readable format, Python’s time and datetime modules have you covered.