Write a Python script to display the various Date Time String formats


  1. Current date and time
  2. Current Date
  3. Current year
  4. Month of year
  5. Week number of the year
  6. Weekday of the week
  7. Day of year
  8. Day of the month
  9. Day of week
  10. Current Time
  11. Current Hour
  12. Current Minute
  13. Current PM / AM
  14. Local Version Date
  15. Local Version Time

The program demonstrates the use of various datetime formatting codes to extract different components of the current date and time. The program starts by importing the datetime module. Then, it gets the current date and time using the datetime.datetime.today() method and stores it in the dt variable.

The program then uses various formatting codes to extract different components of the current date and time, as follows:

  • Current Date and Time: Prints the current date and time.
  • Current Date: Prints only the date component of the current date and time.
  • Current Year: Prints the year component of the current date and time.
  • Month of Year: Prints the month component of the current date and time.
  • Week Number of the Year: Prints the week number of the year of the current date and time.
  • Weekday of the Week: Prints the weekday of the current date and time (0 for Sunday, 6 for Saturday).
  • Day of Year: Prints the day of the year of the current date and time.
  • Day of the Month: Prints the day of the month of the current date and time.
  • Day of Week: Prints the name of the weekday of the current date and time.
  • Current Time: Prints only the time component of the current date and time.
  • Current Hour: Prints the hour component of the current date and time.
  • Current Minute: Prints the minute component of the current date and time.
  • Current PM/AM: Prints whether the current time is AM or PM.
  • Local Version Date: Prints the local date representation.
  • Local Version Time: Prints the local time representation.

The program uses the strftime() method to format the datetime object as per the specified format string for each component of the date and time. The formatting codes used in the program are defined in the Python documentation for the strftime() method of the datetime module.

Source Code

import datetime
dt = datetime.datetime.today()
print("\n1. Current Date and Time :",dt)
print("2. Current Date :",dt.date())
print("3. Current Year :",dt.strftime("%Y"))
print("4. Month of Year :",dt.strftime("%m"))
print("5. Week Number of the Year :",dt.strftime("%W"))
print("6. Weekday of the Week :",dt.strftime("%w"))
print("7. Day of Year :",dt.strftime("%j"))
print("8. Day of the Month :",dt.strftime("%d"))
print("9. Day of Week :",dt.strftime("%A"))
print("10. Current Time :",dt.time())
print("11. Current Hour :",dt.strftime("%H"))
print("12. Current Minute :",dt.strftime("%M"))
print("13. Current PM/AM :",dt.strftime("%p"))
print("14. Local Version Date :",dt.strftime("%x"))
print("15. Local Version Time :",dt.strftime("%X"))

Output

1. Current Date and Time : 2022-09-24 15:32:30.214304
2. Current Date : 2022-09-24
3. Current Year : 2022
4. Month of Year : 09
5. Week Number of the Year : 38
6. Weekday of the Week : 6
7. Day of Year : 267
8. Day of the Month : 24
9. Day of Week : Saturday
10. Current Time : 15:32:30.214304
11. Current Hour : 15
12. Current Minute : 32
13. Current PM/AM : PM
14. Local Version Date : 09/24/22
15. Local Version Time : 15:32:30

Example Programs