Today we are going to share how to get the current date and time in Python. If you are a python beginner and want to start learning the python programming, then keep your close attention in this tutorial as I am going to share how to get the current date and time in Python.
To increase your Python knowledge, practice all Python programs:
Copy the below python program and execute it with the help of python compiler.
now = datetime.datetime.now()
print "Current date and time using str method of datetime object:"
print "Current date and time using instance attributes:"
print "Current year: %d" % now.year
print "Current month: %d" % now.month
print "Current day: %d" % now.day
print "Current hour: %d" % now.hour
print "Current minute: %d" % now.minute
print "Current second: %d" % now.second
print "Current microsecond: %d" % now.microsecond
print "Current date and time using strftime:"
print now.strftime("%Y-%m-%d %H:%M")
Current date and time using str method of datetime object:
Current date and time using instance attributes:
Current year: 2013
Current month: 2
Current day: 17
Current hour: 16
Current minute: 2
Current second: 49
Current microsecond: 338517
Current date and time using strftime:
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.