In Python, the date object from the datetime module represents a date and has the following attributes:
year: the year of the date (as an integer)month: the month of the date (as an integer, from 1 to 12)day: the day of the date (as an integer, from 1 to 31)weekday(): returns the day of the week as an integer, where Monday is 0 and Sunday is 6isoweekday(): returns the day of the week as an integer, where Monday is 1 and Sunday is 7isoformat(): returns the date as an ISO formatted string (YYYY-MM-DD)ctime(): returns a string representing the date and time in a human-readable format (e.g. 'Wed Feb 23 13:38:01 2022')strftime(): returns a string representing the date and time according to a specified format (see the strftime() method for more information on the available format codes)Here's an example of how to create a date object and access its attributes:
|
import datetime
my_date = datetime.date(2022, 2, 23)
print(my_date.year) # 2022
print(my_date.month) # 2
print(my_date.day) # 23
print(my_date.weekday()) # 2 (Wednesday)
print(my_date.isoweekday()) # 3 (Wednesday)
print(my_date.isoformat()) # '2022-02-23'
print(my_date.ctime()) # 'Wed Feb 23 00:00:00 2022'
print(my_date.strftime('%Y/%m/%d')) # '2022/02/23'
|