You're online now.

Hurray! you are online now.

Write a python program to display the current date and time

# Write a python program to display the current date and time.
    import = datetime
    now = datetime.datetime.now()
    print("Current Date and Time : ")
    print(now.strftime("%Y-%m-%d %H:%M:%S")

This code print the Current Date and Time, code uses the ‘datetime’ module in Python to display the current date and time on console.

  1. ‘import datetime’: This line of code imports the ‘datetime' module, which provides classes for working with dates and times in Python.
  2. ‘now = datetime.datetime.now()’: This line of code create a ‘datetime’object called ‘now’, which represents the current date and time. The ‘now()’ method returns the curent date and time as a ‘datetime’ object.
  3. ‘print("Current Date and Time : “)’: This line simply prints the string ”Current Date and Time : " to the console.
  4. ‘print(now.strftime("%Y-%m-%d %H:%M:%S"))’: This line of code prints the current date and time in a specific format. The ‘strftime()’ method is used to format the ‘datetime’ object as a string, using a string format code. The format code "%Y-%m-%d %H:%M:%S" specifies the year, month, and day (in a YYYY-MM-DD format) followed by the hours, minutes, and seconds (in a HH:MM:SS format), separated by hyphens and colons.
🖤 0
Buy me coffee ☕

Comments

Oops!

No comments here...