Python Datetime Date
A datetime.date object represents a date.
Contents
Usage
A datetime.date object is constructed as:
d1 = datetime.date(2020,12,25)
The components can be accessed like:
d1.year # 2020 d1.month # 12 d1.day # 25
Class Functions
FromIsoCalendar
Construct a datetime.date from the year, week, and weekday.
FromIsoFormat
To construct a datetime.date from any valid ISO 8601 string, try:
import datetime datetime.date.fromisoformat('2019-12-04') #datetime.date(2019, 12, 4) datetime.date.fromisoformat('20191204') #datetime.date(2019, 12, 4) datetime.date.fromisoformat('2021-W01-1') #datetime.date(2021, 1, 4)
Note that ordinal dates are not supported by this function.
FromOrdinal
Construct a datetime.date from a string like YYYY-DDD.
FromTimeStamp
Construct a datetime.date from a string representing a POSIX timestamp.
May raise an OverflowError or OSError depending on the local libc (specifically the localtime() implementation).
Today
A datetime.date representing the current date can be constructed like:
d1 = datetime.date.today()
Methods
CTime
Return a string like Wed Dec 4 00:00:00 2002.
Equivalent to time.ctime(time.mktime(d1.timetuple())).
IsoCalendar
Return a named tuple like datetime.IsoCalendarDate(year, week, weekday).
IsoFormat
Return a string like YYYY-MM-DD.
IsoWeekDay
Return the weekday as an integer between 1 (Monday) and 7 (Sunday).
StrFTime
Return a formatted string timestamp.
See here for an explanation of the directives.
Replace
Return a copy of the datetime.date with the specified components replaced.
import datetime d1 = datetime.date(2020, 12, 25) d2 = d1.replace(year=2021) d3 = d2.replace(month=1) d4 = d3.replace(day=31)
TimeTuple
Return a time tuple like time.localtime().
Equivalent to time.struct_time((d1.year, d1.month, d1.day, 0, 0, 0, d1.weekday(), d1.toordinal() - datetime.date(d1.year, 1, 1).toordinal() + 1, -1)).
ToOrdinal
Return the proleptic Gregorian ordinal.
WeekDay
Return the weekday as an integer between 0 (Monday) and 6 (Sunday).