The timedelta Object in Python for Time Differences

The timedelta Object in Python for Time Differences

I write this squished into the train somewhere, running two hours later than I would've liked - but I aspire to be a model student, and I WILL do my homework 😉

This week has seen me to a huge amount of work on timezones - the last one in fact too. I have had very little experience of datetime objects, let alone in Python.

On Thursday I had an AMAZING problem solving sesh with my mentor and we worked on a CodeWars problem called "Minutes To Midnight". We trialled out the format I discussed in my recent post - I worked alone, and came prepared. How did I come prepared? I came with a diagram on what the problem was, what I was stuck on, and what I thought it was I had to do.

It was absolutely amazing and it streamlined our session so much.

I had two main takeaways from the session - the first one was about generally using predefined tools to solve coding problems.

But I wanted to dive deeper into the timedelta object as well. I just want to say - when picking problems to get help with I purposefully went for a level that was commonly too hard for me to solve alone but not too hard for me to get started on. Looking forward to going up a level though!

Images of coding interspersed with texts, in pink, turquoise, green, and yellow. Text reads: the timedelta datetime object. Allows you to denote a span of time and helps you to perform simple calculations on datetime objects

The timedelta Object

The timedelta object is part of the datetime library - therefore to use it, you have to import datetime, and you have to import timedelta from datetime.

Apparently, it is one of the best ways for calculating time differences. I have seen this for myself as well. According to Geeks for Geeks, "it is one of the easiest ways to perform date manipulations." Wait what so there are others??! I knew there are lots of ways to do the same thing in JavaScript - and assumed it would be at least similar in Python too. It always makes me curious what these ways are though!

According to the official Python docs, a datetime class object of type timedelta (Wait what so this is a type of a n instance of a class? OOP is catching up with me! Time to leave behind my FP background). 

In other words, or rather, it also says: "A duration expressing the difference between two date or datetime instances to microsecond resolution." So I am beginning to understand that a datetime object is a class and it can be of different types but I also understand that these objects (are an object and an instance of a class the same thing? don't we call objects dictionaries in Python? Why do I keep getting lost in translation between names in Python and Javascript, occasionally, sometimes?) can be naive or aware - I haven't read all the docs, but I have done some work on this so it kind of makes sense. 

Well it does make sense - I have solved this problem at work with something.

Finally I have found something good

Out of all the articles that I have looked at this one is probably the best so far. It says of the timedelta object: "It denotes a span of time and can help when we need to perform simple arithmetic operations on datetime objects." 

I love this one. I LOVE this one!!! "It denotes a span of time" I love it!!! It is really the word "span" that makes sense. It makes the whole thing click for me. It makes sense. Wow!

Coding Examples

I feel I should do coding examples. Being squashed up on the train doesn't help.

But out of love and respect for my teachers - I will do some coding examples. Here goes. 

Maybe the problem is I'm just not so good at datetime objects. I still don't really get how to create them. I never had this explained clearly to me or never learned it in a course.

Okay, so it's really hard - for example you just create this moment with datetime.now() or datetime.datetime.now() if you haven't imported datetime. But what about if you want to make it UTC? Arrrgh it's all so confusing. 🤔 I've done it before - but can't remember. Oh yeah of course this is the tzinfo argument - more on that later though as I am getting in to London and I have to change soon.




As you can see the first variable creates a datetime object in this moment now - and the second one creates the same object 2 years from now.

The difference is input as an argument to the timedelta method in days - 730 days to be precise. 

I believe that timedelta can take the following arguments:
  • days
  • seconds
  • microseconds
  • milliseconds
  • minutes
  • hours 
  • weeks

But ideally I'd like to be in Pycharm and playing around with this to check... can't connect to the internet well enough to do that right now but this seems right. Here is another example though. 



Notice how I had to change the number of days to get the same date two years ago - probably due to leap year reasons.

Comments

Popular Posts