In this article, we will learn how to get or extract the year, month and day from a DateTime in python.
Example: In this example, we will get the date part from the DateTime
# How to Get the year, month, and day of a datetime in Python # Import Module import datetime now = datetime.datetime.now() print ("\nCurrent date and time using instance attributes:") print ("\nCurrent year: ", now.year) # Output ==> Current year: 2020 print ("\nCurrent month: ", now.month) #Output ==> Current month: 3 print ("\nCurrent day: ", now.day) # Output ==> Current day: 29 print ("\nCurrent hour: ", now.hour) # Output ==> Current hour: 16 print ("\nCurrent minute: ", now.minute) # Output ==> Current minute: 5 print ("\nCurrent second: ", now.second) # Output ==> Current second: 44 print ("\nCurrent microsecond: ", now.microsecond) # Output ==> Current microsecond: 405009
I hope this article will help you to understand how to get or extract the year, month and day from a DateTime in python.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!