All TalkersCode Topics

Follow TalkersCode On Social Media

Python Get Current Time In Seconds

Last Updated : Jan 1, 2023

Python Get Current Time In Seconds

In this tutorial we will show you the solution of python get current time in seconds, in python Programming, we experience various conditions where we have to get the current time and use it as per the need within the code.

This condition occurs many times while developing complex or purpose specific programs in python language.

Uses of the condition :-

  • While tracking time in seconds.
  • Ticketing based programs
  • Digital clock
  • Sports activities and timing tracking based programs etc.

In this tutorial, we’re going to understand the methods of taking current time in python only in seconds unit.

There are many ways to get the current time in Python using the built-in and third-party imported modules within the code.

Python time module provides various functions to get the current time and perform time-related activities so in this tutorial we’re going to understand these methods step by step.

Step By Step Guide On Python Get Current Time In Seconds :-

Method 1 - Using time.time() function –

import time
x = time.time()
print('The current time in seconds is : ', x)
  1. In this code, we used time.time() function.
  2. This function will return the value in float data type by taking the current time stamp, which will eventually show the required condition.
  3. Now, first we imported the module or we can library of time functions within the code, in order to use their features - import time
  4. Now, we declared a variable named ‘x’ and then used time function to get the values - X=time.time()
  5. At the end, we just printed the result and showed the current time in seconds by using print function within the code.
  6. Time function has firstly captured the current time and then converted it into float and then showed us.
  7. This is the very easy and basic way to full fill our condition.

Method 2 - Using time.localtime() function

import time
x = time.localtime(time.time())
print('Minutes:', x.tm_min)
print('Hours:', x.tm_hour)
print('Seconds:', x.tm_sec)
  1. Similarly, we imported the time module within the code on order to use its functions and features.
  2. Now, we declared a variable named ‘x’ and captured the current local time within it - x = time.localtime(time.time())
  3. This function will show the time in a struct_time format.
  4. Now, we printed the minutes, hours and seconds specifically by using the localtime function.
  5. For minutes, we used - print('Minutes:', x.tm_min)
  6. For hours, we used - print('Hours:', x.tm_hour)
  7. For seconds, we used - print('Seconds:', x.tm_sec)
  8. At the end, we eventually got the required result within the code.

Conclusion :-

In this tutorial, we’ve gone through two methods of taking current time in seconds in python programming.

The use of time module is very important in this because it allows the developer to use the required functions in a proper manner.

You can use this condition wherever you want by just following the easy steps mentioned above. I hope this tutorial on python get current time in seconds helps you.

Latest Tutorials