A date in Python is not a data type of its own, but we can import a module named datetime to work with dates as date objects.
import datetimex = datetime.datetime.now() print(x)
The date contains year, month, day, hour, minute, second, and microsecond.
The datetime module has many methods to return information about the date object.
To create a date, we can use the datetime() class (constructor) of the datetime module. The datetime() class requires three parameters to create a date: year, month, day.
import datetimex = datetime.datetime(2020, 5, 17) print(x)
strftime() MethodThe datetime object has a method for formatting date objects into readable strings. The method is called strftime(), and takes one parameter, format, to specify the format of the returned string.
import datetime x = datetime.datetime.now()print("Year:", x.strftime("%Y")) print("Weekday:", x.strftime("%A")) print("Month:", x.strftime("%B"))
%a : Weekday, short version (Wed)%A : Weekday, full version (Wednesday)%w : Weekday as a number 0-6 (0 is Sunday)%d : Day of month 01-31%b : Month name, short version (Dec)%B : Month name, full version (December)%m : Month as a number 01-12%y : Year, short version, without century (18)%Y : Year, full version (2018)