Open In App

Python program to print current hour, minute, second and microsecond

Last Updated : 16 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to discuss how to print current hour, minute, second, and microsecond using Python. In order to print hour, minute and microseconds we need to use DateTime module in Python.

Methods used

  • datetime.now().hour(): This method returns the current hour value of the datetime object.
  • datetime.now().minute(): This method returns the current minute value of the datetime object.
  • datetime.now().second(): This method returns the current second value of the datetime object.
  • datetime.now().microsecond(): This method returns the current microsecond value of the datetime object.

Below are the various implementations using examples that depict how to print current hour, minute, second, and microsecond in python.

Example 1: To print time, hour, minute, second, and microsecond

Python3




# importing datetime module
from datetime import datetime
 
# now is a method in datetime module is
# used to retrieve current data,time
myobj = datetime.now()
 
# printing current hour using hour
# class
print("Current hour ", myobj.hour)
 
# printing current minute using minute
# class
print("Current minute ", myobj.minute)
 
# printing current second using second
# class
print("Current second ", myobj.second)
 
# printing current microsecond using
# microsecond class
print("Current microsecond ", myobj.microsecond)


Output

Current hour  4
Current minute  5
Current second  13
Current microsecond  121302

Time complexity: O(1) – constant time complexity, since the code is only retrieving and printing the current time values.
Auxiliary space: O(1) – constant space complexity, since the code is not using any additional data structures or variables that increase with input size.

Example 2: To print object, time, hour, minute, second, and microsecond.

Python3




# importing datetime module
from datetime import datetime
 
# now is a method in datetime module is
# used to retrieve current data,time
myobj = datetime.now()
 
 
# printing the object itself
print("Object:", myobj)
 
# printing current hour using hour
# class
print("Current hour ", myobj.hour)
 
# printing current minute using minute
# class
print("Current minute ", myobj.minute)
 
# printing current second using second
# class
print("Current second ", myobj.second)
 
# printing current microsecond using microsecond
# class
print("Current microsecond ", myobj.microsecond)


Output

Object: 2022-11-24 04:07:13.707730
Current hour  4
Current minute  7
Current second  13
Current microsecond  707730

Time Complexity: O(1)
Auxiliary Space: O(1)

Approach#3: Using time module

We can use the built-in time module in Python to get the current time. We can use the time() function of the time module to get the number of seconds and then extract the hour, minute, second, and microsecond components from it.

Algorithm

1. Import the time module.
2. Call the time() function of the time module to get the current time.
3. Convert the current time to a struct_time object using the localtime() function of the time module.
4. Extract the hour, minute, second, and microsecond components from the struct_time object.
5. Print the extracted components.

Python3




import time
 
current_time = time.time()
local_time = time.localtime(current_time)
 
hour = local_time.tm_hour
minute = local_time.tm_min
second = local_time.tm_sec
microsecond = int((current_time - int(current_time)) * 1000000)
 
print('current hour' ,hour)
print('current minute' ,minute)
print('current second ',second)
print('current microsecond ', microsecond)


Output

current hour 10
current minute 48
current second  50
current microsecond  397817

Time complexity: O(1)
Auxiliary Space: O(1)

Approach#4: Using strftime()

Use the time module to get the current time in seconds since the Epoch. Use the strftime function to format the time into hours, minutes, seconds, and microseconds. Convert the fractional part of the seconds into microseconds by subtracting the integer part and multiplying by 1,000,000. Print the formatted time.

Algorithm

1. Get the current time in seconds since the Epoch using time.time() function.
2. Use the time.strftime() function to convert the current time into the desired format.
3. Convert the fractional part of seconds into microseconds.
4. Print the formatted time.

Python3




import time
 
now = time.time()
hour, minute, second = time.strftime('%H'), time.strftime('%M'), time.strftime('%S')
microsecond = int((now - int(now)) * 1000000)
print(f"Current hour: {hour}")
print(f"Current minute: {minute}")
print(f"Current second: {second}")
print(f"Current microsecond: {microsecond}")


Output

Current hour: 05
Current minute: 17
Current second: 27
Current microsecond: 760716

Time complexity: O(1), as all operations take constant time.
Auxiliary Space: O(1), as we only store a few variables that have constant size.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads