Tags
python
Asked 1 years ago
26 Apr 2023
Views 164
david

david posted

How to print current date and time in python?

want to print current date and time in python , how to do it ?
iptracker

iptracker
answered May 1 '23 00:00

coder can print the current date and time using the datetime module . Here's an example:


import datetime

now = datetime.datetime.now()
print("Current date and time:", now)

The datetime.now() method returns a datetime object representing the current date and time . This object can be printed using the print() function.

If you want to print the date and time in a specific format, you can use the strftime() method of the datetime object. Here's an example:


import datetime

now = datetime.datetime.now()
formatted_date = now.strftime("%Y-%m-%d %H:%M:%S")
print("Formatted date and time:", formatted_date)

In this example, the %Y-%m-%d %H:%M:%S string passed to the strftime() method represents the format in which you want to print the date and time . %Y represents the year, %m represents the month, %d represents the day, %H represents the hour, %M represents the minute, and %S represents the second. The resulting string will have the date and time in the specified format.
Post Answer