Software engineering and personal development

Tag: date

10 Quick Python Snippets That Can Help You Work with Dates

Photo by Brad Neathery 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.

It can be doing a conversion from one format to the other one, changing the timezone, etc.

Since you have already decided to read this article, then why don’t we stop wasting time talking and just start immediately learning?

1. Convert a string into a date

Despite being engineers, we still spend a considerable amount of time using a natural language to talk.

Since that is the case, we can also convert a string looking like a natural phrase into a date as shown below:

In this example:

  • %d — represents the day of the month, like 21, 22, 23, etc.
  • %B — represents the actual name of the month like January, February, March etc.
  • %Y — represents the year, like 2020, 2021, etc.

2. Convert from one timezone to another one

As we are mostly working remotely these days, we may have the chance to also work with someone who is in another timezone.

You can of course just Google and find out what time it is now in New York City even if you have never been before, but there is joy in implementing this yourself.

Let’s see how to do that.

3. Find the number of days between 2 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.

To do that quickly, we can import the date module in our Python script and initially prepare the dates:

After that, we can basically just find the difference between 2 numbers:

Yes, it’s that simple.

You do not have to spend hours doing it manually on a physical calendar. It can take barely a few moments to do it like this.

4. Add days to a date

Let us imagine that you are working on a feature of a product in which the subscription for a user expires 90 days from now. You want to save up that date in the database using the following:

5. Add weeks to a date

You can also just change that to weeks, like the following:

6. Subtract days from a date

Have you ever wanted to go back in time and improve something in your life, stayed more with your loved ones, or simply start investing in Bitcoin a little bit earlier?

Well, I wish I could tell you how to do that.

However, if you want to find a date in the past, let’s say 180 days from now, you can easily do that using the following:

7. Subtract weeks from a date

As you may have guessed already, you can also find a date by subtracting weeks:

8. Find the difference between 2 dates in months

We can also find the difference between the 2 dates in months quite quickly.

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 months.

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

9. Put days before months

The majority of the world puts days in front of months in their dates:

Image: John Harding/Mona Chalabi Photograph: John Harding/Mona Chalabi

However, there can be cases when you get as input a date where the month is in front of the day.

If you want to convert those dates in the format that the majority of the world uses, here is how:

10. Find the first day of the month

In some companies, they pay their employees on the first day of every month.

If you want to know what day of the week will that be, then this snippet can be helpful:


That’s pretty much it. I hope you learned at least something in this article.

How to Convert a Natural-language-like String into a Date in Python

turned on gray laptop computer
Photo by Oskar Yildiz on Unsplash

You have probably come across situations in which you needed to convert a string into a date in Python. There are a few ways to do that, but you may have not had the chance to use the following method.

It’s not that it is so special that you cannot learn, or that it is so difficult what’s going on. It’s basically a rare conversion that you have not had the chance to stumble upon before.

Continue reading

© 2024 Fatos Morina

Theme by Anders NorenUp ↑