Write a Python program to get the current date time information


The program is a Python code that uses the built-in datetime module to get the current date and time, and then extract and display different components of the date and time separately. Here is a step-by-step explanation of the program:

  • import datetime: This line imports the built-in datetime module in Python that provides classes for working with dates and times.
  • dt = datetime.datetime.now(): This line creates a datetime object dt that represents the current date and time by calling the now() method of the datetime class. The now() method returns the current date and time as a datetime object.
  • print("Current date and time : " , dt) : This line prints the current date and time using the print() function. The output will be a string that contains the current date and time in the format YYYY-MM-DD HH:MM:SS.ssssss, where YYYY is the year, MM is the month, DD is the day, HH is the hour, MM is the minute, SS is the second, and ssssss is the microsecond.
  • print("---------------------DATE---------------------"): This line prints a separator string to separate the date components from the time components that will be printed later.
  • print("Date : " ,dt.date()): This line prints the date component of the current date and time by calling the date() method of the datetime object dt. The output will be a string that contains the date in the format YYYY-MM-DD.
  • print("year : ", dt.strftime("%Y")): This line prints the year component of the current date and time by calling the strftime() method of the datetime object dt with the format string %Y. The %Y format specifier represents the year with century as a decimal number.
  • print("Month of year: ", dt.strftime("%B")): This line prints the month component of the current date and time by calling the strftime() method of the datetime object dt with the format string %B. The %B format specifier represents the full month name.
  • print("Day of the month : ", dt.strftime("%d")): This line prints the day component of the current date and time by calling the strftime() method of the datetime object dt with the format string %d. The %d format specifier represents the day of the month as a zero-padded decimal number.
  • print("Day of week : ", dt.strftime("%A")): This line prints the day of the week component of the current date and time by calling the strftime() method of the datetime object dt with the format string %A. The %A format specifier represents the full weekday name.
  • print("Weekday of the week : ", dt.strftime("%w")): This line prints the weekday of the week component of the current date and time by calling the strftime() method of the datetime object dt with the format string %w. The %w format specifier represents the weekday as a decimal number, where 0 is Sunday and 6 is Saturday.
  • print("Day of year : ", dt.strftime("%j")): This line prints the day of the year component of the current date and time by calling the strftime() method of the datetime object dt with the format string %j. The %j format specifier represents the day of the year as a zero-padded decimal number.
  • print("Week number of the year : ", dt.strftime("%W")): Prints the week number of the year of the datetime object, using the %W format code
  • print("----------------------TIME--------------------"): This line prints another separator line to the console, to separate the time-related information from the date-related information.
  • print("Time: " ,dt.time()): Prints the time portion of the datetime object.
  • print("Hours : ", dt.strftime("%H")): Prints the hour of the datetime object in 24-hour format, using the %H format code.
  • print("Minutes : ", dt.strftime("%M")): Prints the minute of the datetime object, using the %M format code.
  • print("Seconds : ", dt.strftime("%S")): Prints the second of the datetime object, using the %S format code.
  • print("PM / AM : ", dt.strftime("%p")): Prints "AM" or "PM" depending on whether the time of the datetime object is before or after noon, using the %p format code.

Source Code

import datetime
dt = datetime.datetime.now()
print("Current date and time : " , dt)
print("---------------------DATE---------------------")
print("Date : " ,dt.date())
print("year : ", dt.strftime("%Y"))
print("Month of year: ", dt.strftime("%B"))
print("Day of the month : ", dt.strftime("%d"))
print("Day of week : ", dt.strftime("%A"))
print("Weekday of the week : ", dt.strftime("%w"))
print("Day of year : ", dt.strftime("%j"))
print("Week number of the year : ", dt.strftime("%W"))
print("----------------------TIME--------------------")
print("Time: " ,dt.time())
print("Hours : ", dt.strftime("%H"))
print("Minutes : ", dt.strftime("%M"))
print("Seconds : ", dt.strftime("%S"))
print("PM / AM : ", dt.strftime("%p"))

Output

Current date and time :  2022-09-24 13:43:12.697179
---------------------DATE---------------------
Date :  2022-09-24
year :  2022
Month of year:  September
Day of the month :  24
Day of week :  Saturday
Weekday of the week :  6
Day of year :  267
Week number of the year :  38
----------------------TIME--------------------
Time:  13:43:12.697179
Hours :  13
Minutes :  43
Seconds :  12
PM / AM :  PM

Example Programs