How to get the current date and time in python?
In this article, python gets today’s date and current date and time. the date and time in different formats using strftime() function. you can use the date.today() method to get the current local date. Now, you can use the strftime() method to create a string representing a date in different formats.
datetime.now() to get the current date and time. you use strftime() to create a string representing date and time in another format.
Here example of how to get the current date and time in Python :
import datetime
now = datetime.datetime.now()
print ("Get current date and time:%s" % (now))
#output
Get current date and time:2020-05-25 22:34:05.308365
print ("Current year: %d" % (now.year))
#output
Current year: 2020
print ("Current month: %d" % (now.month))
#output
Current month: 5
print ("Current day: %d" % (now.day))
#output
Current day: 25
print ("Current hour: %d" % (now.hour))
#output
Current hour: 22
print ("Current minute: %d" % (now.minute))
#output
Current minute: 34
print ("Current second: %d" % (now.second))
#output
Current second: 5
print ("Current microsecond: %d" % (now.microsecond))
#output
Current microsecond: 308365
print ("Strftime date and time :%s" % (now.strftime("%Y-%m-%d %H:%M")))
#output
Strftime date and time :2020-05-25 22:34