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:
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.
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"))
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
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions