Software engineering and personal development

Month: March 2021

How to Quickly Find the Difference Between 2 Dates in Months in Python

laptop on brown wooden table
Photo by XPS on Unsplash

Finding the number of days is usually quite popular and well documentated. There can also be cases when we need to find a difference in terms of weeks or even months.

If you want to find the difference in months between 2 dates, then here is a quick Python script to help you do that.

For example, between October 2018 and November 2021, there isn’t just a month difference. There is also a difference in years as well.

Let us assume that we don’t have dates prepared yet, so we initialize 2 variables with 2 dates.

After that, we find the difference in terms of number of months in between.

We should keep in mind that we may have dates that are in different years, and it’s not enough to think that we can just subtract two dates.  Because of that, we need to also find the difference in years.

Here is the entire script:

from datetime import datetime
first_date = datetime(2021, 10, 26)
second_date = datetime(2020, 5, 26)
month_difference = first_date.month - second_date.month
year_difference = first_date.year - second_date.year
month_difference = month_difference + 12 * year_difference

You may get the impression that we may have a negative number in the end, since we are subtracting the number representing a month from another month.

To illustrate, let us assume that we have the following:

first_date = datetime(2021, 3, 26)
second_date = datetime(2020, 12, 26)

Now, since we are subtracting the number of months from the first date and 3 < 12, we will indeed get -9. However, we also have in the end a difference in years that is equal 1, since 2021 – 2020 = 1.

Hence, we will add 12 to -9, and in the end we get the result equal to 3:

from datetime import datetime
first_date = datetime(2021, 3, 26)
second_date = datetime(2020, 12, 26)
month_difference = first_date.month - second_date.month # -3
year_difference = first_date.year - second_date.year # 1
month_difference = month_difference + 12 * year_difference # -9 + 12 * 1

That’s pretty much it.

I hope you find this useful.

Why Software Engineers Need to Know Their Way Around Hardware

Image source: https://images.pexels.com/photos/6804581/pexels-photo-6804581.jpeg


When we code programs, hardware is often the last thing we think about. After all, we have intuitive compilers to handle system translations for us. They can even check our errors for free. Plus, if our computers run the programs perfectly, then there’s no need to think about how they do it.

However, this couldn’t be further from the truth. Here’s why software engineers need at least basic hardware knowledge.

Continue reading

How To Quickly Find The Number Of Days Between 2 Dates In Python

laptop on brown sofa
Photo by XPS on Unsplash

If you use Python in your work, or simply for scripting, chances are that there will come a point in time when you are going to need to work with dates.

Since we still live in this universe where time passes, we may need to find the number of days that have passed between one point and another one.

We can of course pull up a calendar and manually count the days, which is slow and boring.

Continue reading

Reduce RAM usage by your browser with AutoTabDiscard

man in brown jacket looking at contract on DocuSign on a laptop
Photo by DocuSign on Unsplash

If you are reading this article, can you please take a second and count the number of tabs that you have currently as opened?

If you are like me, chances are that this number should be greater than 5.

All these tabs can be a bit distracting, since you may consider opening them up and checking them from time to time even if you do not need to do that.

Moreover, all these tabs can consume RAM in your computer.

Continue reading

© 2024 Fatos Morina

Theme by Anders NorenUp ↑