Python Get Current Date And Time
Chapter:
Python
Last Updated:
19-06-2023 17:24:07 UTC
Program:
/* ............... START ............... */
# To get the current date and time in Python, you can use the datetime module.
from datetime import datetime
current_date_time = datetime.now()
print(current_date_time)
# display the date and time in a specific format, you can use the strftime method
from datetime import datetime
current_date_time = datetime.now()
formatted_date_time = current_date_time.strftime("%Y-%m-%d %H:%M:%S")
print(formatted_date_time)
/* ............... END ............... */
Output
2023-06-19 15:42:23.987561
2023-06-19 15:42:23
Notes:
-
To get the current date and time in Python, you can use the datetime module.
- If you want to display the date and time in a specific format, you can use the strftime method.
- In the strftime method, you provide a format string that specifies the desired format for the date and time. The %Y, %m, %d, %H, %M, and %S are format codes representing the year, month, day, hour, minute, and second, respectively.