Reading Time: 1 minutes
Getting current date in Python
The date class of Python's builtin datetime module helps us to retrieve current date.
>>> import datetime >>> thisDay = datetime.date.today() >>> type(thisDay) <class 'datetime.date'> >>> thisDay datetime.date(2016, 12, 24) >>> print(thisDay) 2016-12-24 >>> thisDay.__repr__() 'datetime.date(2016, 12, 24)' >>> thisDay.__str__() '2016-12-24' # In order to convert the date object generated by today() function into string, use the strftime() function. You will need to specify the format of your date with the help of directives. The following format uses three standard directives, namely %d, %m and %y for day, month and year without century, respectively. >>> thisDay.strftime("%d/%m/%y") '24/12/16' # The following format uses # %A: full name of the weekday e.g Sunday, Monday etc. Use %a for the abbreviated versions like Sun, Mon etc. # %d: the date number e.g 01, 02, 22 etc. # %B: full name of month e.g. January, February. Use %b for abbreviated versions like Jan, Feb etc. # %Y: year with century e.g. 2015, 2016. Use %y as in the earlier format, for year without century. >>> thisDay.strftime("%A %d %B %Y") 'Saturday 24 December 2016'
You can find the full list of directives on the official Python datetime documentation.