Python Date Now
Chapter:
Python
Last Updated:
23-07-2023 14:54:56 UTC
Program:
/* ............... START ............... */
from datetime import datetime
# Get the current date and time
current_date_time = datetime.now()
# Print the current date and time
print(current_date_time)
/* ............... END ............... */
Output
2023-07-23 12:34:56.789012
Notes:
-
This Python program uses the datetime module to retrieve the current date and time. The datetime module is a part of Python's standard library and provides functions to work with dates and times.
- In the program, we start by importing the datetime class from the datetime module:
- Next, we use the datetime.now() function to get the current date and time. This function returns a datetime object representing the current date and time at the moment when the function is called:
- Finally, we print the current_date_time variable to display the current date and time in the console:
- The output will vary depending on the actual date and time when you run the program. It includes the year, month, day, hour, minute, second, and microsecond components, separated by dashes, spaces, and colons.