Home »
Python »
Python Programs
Print current hour, minute, second, and microsecond in Python
In this tutorial, we will learn how to get and print current hour, minute, second, and microsecond using Python program?
By IncludeHelp Last updated : April 21, 2023
In the below example – we are implementing a Print current hour, minute, second, and microsecond in Python.
Steps to print current hour, minute, second, and microsecond
- Import the datetime class from datetime module.
from datetime import datetime
- Create an object by calling the now() function of datetime class.
obj_now = datetime.now()
- Extract and print the current hour, minute, second and microsecond using the object of datetime.now().
obj_now.hour
obj_now.minute
obj_now.second
obj_now.microsecond
Python program to print current hour, minute, second, and microsecond
# Python program to print current hour,
# minute, second and microsecond
# importing the datetime class from
# datetime module
from datetime import datetime
# creating object
obj_now = datetime.now()
# printing the current date and time
print("Current date & time: ", obj_now)
# extracting and printing the current
# hour, minute, second and microsecond
print("Current hour =", obj_now.hour)
print("Current minute =", obj_now.minute)
print("Current second =", obj_now.second)
print("Current microsecond =", obj_now.microsecond)
Output
Current date & time: 2020-03-09 08:52:48.262154
Current hour = 8
Current minute = 52
Current second = 48
Current microsecond = 262154