How to get Python current date and time

This is an example on how to get Python today or current date and time.

import datetime

now = datetime.datetime.now()

#I will use print() instead of using print in order to working well with Python 3
print("Current date and time using str method of datetime object:")
print(str(now))
# result: 2014-09-26 16:34:40.278298 

print("Current date and time using instance attributes:")
print("Current year: %d" % now.year)
# result: 2019

print("Current month: %d" % now.month)
# result: 05

print("Current day: %d" % now.day)
# result: 20 

print("Current hour: %d" % now.hour)
# result: 16

print("Current minute: %d" % now.minute)
# result: 43 

print("Current second: %d" % now.second)
# result: 30 

print("Current microsecond: %d" % now.microsecond)
# result: 278298

print("Current date and time using strftime:")
print(now.strftime("%Y-%m-%d %H:%M"))
# result: 2019-05-20 16:34 

print("Current date and time using isoformat:")
print(now.isoformat())
# result: 2019-05-20T16:34:40.278298 

Happy coding!