Write a Python program to Print hour, minute, second, microsecond, day, month, year Given datetime


This program demonstrates how to extract various components of the current date and time using the datetime module in Python. The first line of code imports the datetime module. The second line creates a datetime object representing the current date and time and assigns it to the variable dt.

The next few lines extract various components of the current date and time from the datetime object and print them to the console using the hour, minute, second, microsecond, day, month, and year attributes of the datetime object.

For example, dt.hour gives the current hour of the day, dt.minute gives the current minute, dt.second gives the current second, dt.microsecond gives the current microsecond, dt.day gives the current day of the month, dt.month gives the current month, and dt.year gives the current year.

Source Code

import datetime
dt = datetime.datetime.now()
print("Date and Time :",dt)
print("Hour :",dt.hour)
print("Minute :",dt.minute)
print("Second :",dt.second)
print("Microsecond :",dt.microsecond)
print("Day :",dt.day)
print("Month :",dt.month)
print("Year :",dt.year)

Output

Date and Time : 2022-09-24 15:31:53.192779
Hour : 15
Minute : 31
Second : 53
Microsecond : 192779
Day : 24
Month : 9
Year : 2022

Example Programs