Python Datetime Datetime
A datetime.datetime object represents a date and time.
Usage
A datetime.date object is constructed as:
d1 = datetime.datetime(2020, 12, 31, 23, 59, 59, 999999, tzinfo=None)
While the year, month, and date are required, the remaining options are optional.
d2 = datetime.datetime(2020, 12, 31)
The components can be accessed like:
d1.year # 2020 d1.month # 12 d1.day # 25 d1.hour # 23 d1.minute # 59 d1.second # 59 d1.millisecond # 999999 d1.tzinfo # None d1.fold # 0
Note that fold is used to indicate a time that is 'repeated' (as in 'setting the clock back').
Class Functions
Combine
FromIsoCalendar
FromIsoFormat
FromOrdinal
FromTimestamp
Now
StrPTime
Today
UtcFromTimeStamp
UtcNow
Methods
AsTimeZone
CTime
Date
Dst
IsoCalendar
IsoFormat
IsoWeekDay
Replace
StrFTime
Time
TimeStamp
TimeTuple
TimeTz
ToOrdinal
TzName
UtcOffset
UtcTimeTuple
WeekDay
Operations
datetime.datetime objects can be used with the following operators and functions.
Operation |
Meaning |
d1 + timedelta |
returns a new datetime representing d1 incremented by the duration |
d1 - timedelta |
returns a new datetime representing d1 decremented by the duration |
d1 - d2 |
returns a datetime.timedelta |
d1 < d2 |
returns True if d1 is earlier than d2 else False |
Two datetime.datetime objects can only be used in operations together if both have timezone information, or both are naive to timezones. Mixing categories will raise a TypeError.