From the course: Python Standard Library Essential Training

Unlock the full course today

Join today to access over 22,700 courses taught by industry experts or purchase this course individually.

Calculating dates and times

Calculating dates and times - Python Tutorial

From the course: Python Standard Library Essential Training

Start my 1-month free trial

Calculating dates and times

- All right, let's wrap up our investigation of dates and times by seeing how we can perform calculations on these types. So here in dtcalc start I have two date time objects that are five days and five hours apart. So one is for 10 a.m. and one is for 3 p.m. using a 24 hour clock. So the time Delta class is used to represent a span of time rather than a specific time. And this is the class that we'll use to add and subtract time from other time based objects. But first let's see how dates and times can be compared to each other just like you can with regular numbers. So I'm going to print dt1 is less than dt2, and then I'll do the other way. Right. Okay. So I'm going to compare these two dates and times to each other. So let's run that. And you can see in the result that date one comes before date two so that output is true. And then obviously the converse of that is going to be false. So let's clear that. We could also subtract one date from another in order to create a time delta…

Contents