Time-related tasks are performed on Python using the time module. With this module the time value can be displayed in different ways. The time.time() method of this module is used to read the time in seconds based on time conventions. Depending on the time period, the calculation of the time starts on date 1. January 1970, 00:00:00 (UTC) and supports date and time until 2038 for most operating systems. Time(), the module returns the time in seconds as a floating point number. This tutorial explains how this method can be used with other necessary methods of the time module to display the time value in different formats. My website AWS Cloud management.

Syntax:

This method has no arguments and returns time in seconds, since the time of the beginning of an era is a floating point number. In the next part of the lesson, different ways to use the timdd() method are presented.

Example 1: Using the time() function with ctime() to print the current date and time

Earlier it was mentioned that the time() method returns a value in seconds and is a floating point number that is not readable. The ctime() method is used here to display the return value of the time() method in a readable format. Import time and ctime from the time module at the beginning of the script to use the time() and ctime() methods. The script saves the current date and time in seconds in a variable called current_DateTime using the time() method. The value current_DateTime is then printed. The value of this variable is passed as an argument to the ctime() method to convert it to a human-readable format and print it.

# Import time and c-time from module
of the time import time, c-time

# Read current data and time in seconds
current_DateTime = time()

# Print time output()
print(nPrint time output():,current_DateTime)

# Print the current date and time in readable format
print (‘nToday is: ‘,ctime(current_DateTime))

A way out:

After executing the script, the following output appears.

How to use python time.time() method

Example 2: Using the time() function with localtime() to print the current date and time separately.

In the previous example, the date and time values are printed as a string, which is the default output of the ctime() method. However, if you want to read each data and time value and print each value in a custom format, you must use another method called localtime() with the time() method. The local timdd() method accepts the output of the timdd() method as an argument and displays the structure of the date and time values that can be read separately. The following example shows how to read and print different parts of the current data and time using the methods time() and localtime() The time module is imported at the beginning of the script to use the methods time() and localtime(). The output of the time() method is stored in a curTime variable, and the output of the localTime() method is stored in the localTime variable. The value of the variable localTime is printed to see the structural output of this variable. Then the list of months and the list of weekday variables, i.e. the names of the month and the day of the week, based on the numerical value specified in the output of the local timee(() method. Finally, the script generates four types of formatted output data and times.

#! /usr/bin/env python3
# Module import time
Import time

# Read the current time in seconds
curTime=time.time()

# Read data and time values using localtime()
localTime = time.localtime (curTime)

# Localtime()
print (The localtime() output is :n,localTime)

# Define a list of months
months = [January, February, March, April, May, June, July,
August, September, October, November, December].

#
Days of the week = [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday] Print (The formatted output is as follows:)

# Print today’s date
(nDate: , localTime.tm_mday, months [localTime.tm_my-1], localTime.tm_year)

# Print current time
print(nTime: %dh:%dm:%ds %(localTime.tm_hour,localTime.tm_min,localTime.tm_sec)))

# Print the day of the week
(nToday is , days of the week [localTime.tm_wday])

# Print day of year
(nDay is %d days of the year %localTime.tm_day)

A way out:

After executing the script, the following output appears.

How to use python time.time() method

Example 3: Using the time() function with localtime() and strftime() to print the date and time.

There is another method in Python to read date and time values using different types of format codes called strftime(). The time(), localtime() and strftime() methods are used in the following script to create more specific formatted date and time values than in the two previous examples. The time module is imported at the beginning of the script to use the three methods mentioned here. First, the output of the timdd() method is passed as an argument to the localtime() method, then the strftime() method uses the output of the localtime() method with formatting codes in different ways to produce different types of output. In Python, there is no direct format code to add a tag suffix as in other programming languages (1, 2, 3 and th). Here the function with the suffix name is defined to add a suffix with the value of the date per day.

#! /usr/bin/env python3
# Module import time
Import time

# Read the current date and time
currentDT = time.localtime(time.time()).

# Read the day of the month
day = int(time.strftime(%d, currentDT))

# Determine the function for setting the suffix for day
(day):
as ((day > 3 and day <= 20) or (day > 23 and day <= 30)). :
Suffix = the
different :
Suffix = [st, nd, rd] [day % 10 – 1] Yield suffix

# Date display in abbreviated print
(abbreviated date:, time.strftime (%d-%m-%Y, currentDT))

# Show long date
print (time.strftime (long date: %A, %d + suffix (day) + %B %Y, currentDT)).

# Show short time
print (time.strftime (short time: %H:%M:%S,currentDT)).

# Show long pressure
(time.strftime (long time: %I:%M:%S %p, currentDT)).

A way out:

The following output is displayed after the script has been executed.

How to use python time.time() method

Conclusion:

There are many temporary methods in the Python time module. The use of the timdd() method with two other useful Python time methods is shown in this lesson. I hope this tutorial will help readers learn how to use the time-() method.