Python program to print current hour, minute, second and microsecond
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 modulefrom datetime import datetime# now is a method in datetime module is# used to retrieve current data,timemyobj = datetime.now()# printing current hour using hour# classprint("Current hour ", myobj.hour)# printing current minute using minute# classprint("Current minute ", myobj.minute)# printing current second using second# classprint("Current second ", myobj.second)# printing current microsecond using# microsecond classprint("Current microsecond ", myobj.microsecond) |
Output
Current hour 4 Current minute 5 Current second 13 Current microsecond 121302
Example 2: To print object, time, hour, minute, second, and microsecond.
Python3
# importing datetime modulefrom datetime import datetime# now is a method in datetime module is# used to retrieve current data,timemyobj = datetime.now()# printing the object itselfprint("Object:", myobj)# printing current hour using hour# classprint("Current hour ", myobj.hour)# printing current minute using minute# classprint("Current minute ", myobj.minute)# printing current second using second# classprint("Current second ", myobj.second)# printing current microsecond using microsecond# classprint("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)





Please Login to comment...