Let's review what we've learned.
Remember: To work with
datetime objects, you must first import the
To create objects from the
datetime module, use:
time_object = datetime.time(9, 30, 0)
date_object = date(2018, 12, 1)
datetime_object = datetime.datetime(2018, 12, 1, 9, 45, 0)
To get a specific part of an object, use the dot operator:
Objects from the
datetime module are immutable, but you can create a new object from an existing one:
new_time_object = time_object.replace(minute=45)
isoweekday() functions return an integer representing the day of the week for a given date or datetime.
weekday() function returns an integer from 0 (Monday) to 6 (Sunday).
isoweekofday() function is similar, but returns a value from 1 (Monday) to 7 (Sunday) instead.
When you subtract two
datetimes, you get a
timedelta. You can also create a
timedelta and add it to or subtract it from a
sample_timedelta = datetime.timedelta(days=1)
- You can compare objects from the
datetime module using
==. Remember that
datetime_a > datetime_b is
datetime_a is a later (more recent) point in time than
- To format a datetime object, use
Ready for a short quiz before moving on?