Python

How to Print the Current Time in Python

Python is a very vast programming language. It includes many methods to print the current time of our programming machine. The major modules that Python have to print the current time are the “time” and “datetime” modules. In this article, we will discover the ways to get the current time of our region using the Python laws. We can retrieve the current time in Python using several methods. We can easily access the time of our locale or any other different zone using the Python modules.

We will observe the usage of “datetime.now()”, “datetime.utcnow()” and “time.gmtime()” methods to print the current time of our respective zones. The “pytZ” module can be used to figure out the current time in a certain time zone.

Syntax:

The syntax to print the current time by “datetime.now()” is given in the following:

The syntax to obtain the current time includes two parts where the first part of the syntax is the definition of the function which holds the current time. After the function definition, the second part holds the call of the “datetime.now()” function along with a library to reach the current time.

Example 1: Using the Datetime.Now() Method to Print the Current Time and Date

Date and time data types are incorporated into a single Python data type, but there is a module of DateTime which may be programmed to use the time and date. It isn’t necessary to install the DateTime module externally because it is available in the built-in form. To obtain the current date and time, we use the DateTime module’s “datetime.now()” function.

We will discuss our code snippet of the “datetime.now()” function from here.

We start our code by importing the “datetime” library. Then, we create our function named “Currently_Time” that holds the current time in it. Next, we apply the “datetime.now()” function on the “datetime” library within the “Currently_Time” function. The “now” in the “datetime.now()” function refers to the current time. After the formation of the function, we use the “print()” function to print the time on the output where “\n” refers to the cursor to the next line. It displays the current time on the output displays that is stored in the “Currently_Time” function.

The output displays the message that we provide in the print statement in the first line which is “The Current Time at Scotland Glasgow is:” The last line displays the current date and time in Scotland where the date is “2022-11-03” and the time is “09:38:35.068618” which can be seen in the output display.

Example 2: Using the Datetime.Now() Method Along with Multiple Attributes to Print the Current Date and Time

In this example, we will examine some conveys of the “datetime.now()” function. Item bodies includes “year”, “month”, “day”, “hour”, “minute” and “second”. To view the current time separately in Python in separate rows, we use these attributes of the “datetime.now()” method.

The explanation of attributes of the “datetime.now()” method code snippet starts from here.

As in the previous example, we first import the “datetime” library and create a function named “Python_Time”. Then, we apply the “datetime.now()” method on the “datetime” library within the function. The first line of the “print()” function is just a simple print statement that we use to show in the “Following Are The Attributes Of datetime.now() Function:” display. Now, it’s time to use the attributes of the “datetime.now()” along with the print statements within the “print()” function and store the current year by “Python_Time.year”.

To have the current month, we apply the “Python_Time.month”. To display the current day, we use the “Python_Time.day” in our function to store the current day value. We use the “Python_Time.hour” for the current hour time. We use the “Python_Time.minute” for the current minutes. And we use the “Python_Time.second” for current seconds in our “Python_Time” function.

Our code displays the current time in several separate rows as we can see in our snapshot. The first output displays the current year, “2022”. In the third row, it displays the current month in the numeric form which is “11”. In the fourth row, it displays the current day which is “3”. The fifth row displays the current hour which is “9”. Sixth row of the output displays the current minutes which is “56”. And the last row of the output displays the current second which is “30”.

Example 3: Using the Datetime.Utcnow() Method to Print the Current Time

The “datetime.utcnow()” method determines the current universal coordinated time as if it is used along with “now()”. Times are reported in local time along with an offsetting in hours and minutes for the time zone. When the date and time are in a local time zone, that is in “hh” hours and “mm” minutes ahead of universal coordinated time which is a time zone of “+hh:mm”.When we are engaging with applications that have a global user for event logging, this method of the current time is helpful. The datetime.utcnow() method can be used to locate the current time in a universal coordinated time. The coordinated universal time is a time of 24 hours format.

In our code snippet, we can see that we first imported the “datetime” library from “datetime”. Then, we used the “print()” function as we want to display the current coordinated universal time on our display screen. To attain the current coordinated universal time, we just simply used the “datetime.utcnow()” function and assigned the value to the “print()” function.

The output snapshot displays the coordinated universal time along with the current date which is “2022-11-03” and the current time which is “17:28:35.974217”.

Example 4: Using the Time.Gmtime() Method to Print the Current Time

The “time.gmtime()”function refers to the Greenwich meantime. Usually, it is denoted as “GMT”. In this method, the current time that we require should always be passed in “seconds”. This time refers to the time zone at zero degrees longitude, which passes through the London suburb of Greenwich. It is known by the acronym of “zero meridians”.

Let us discuss our code snippet where we import the library of “time” in the first line. After a while, we create our function name “Green_Mean_Time” where we assign the current GMT using the “time.gmtime(time.time())” function. After applying the main function, we use the “print()” function to display the current Greenwich mean time by assigning the “print()” function value of the pre-defined “Green_Mean_Time” function.

The first line of the output display shows the print statement that we provided in the code for the “print()” function. The Greenwich mean time is displayed from the second line of the output snapshot where we can see the current time in “time.struct_time” where the current Greenwich mean time for “tm_year” is “2022”, “tm_month” is “11”, “tm_mday” is “3”, “tm_hour” is “17”, “tm_min” is “47”, “tm_sec” is “51”, “tm_wday” is “3”, “tm_yday” is 307” and “tmisdst” is “0”.

Conclusion

This article discusses the use of different Python methods to print the current time of our respective zones. There are different techniques for such purposes and the major used ones that we applied are “datetime.now()”, “datetime.utcnow()”, and “time.gmtime()”. The “datetime.now()” is used to obtain the current time and date of the local zone. The “datetime.utcnow()” is used to obtain the current time of global users for their events logging. And the “time.gmtime()” is used to obtain the current Greenwich mean time in seconds.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.