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:
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.
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.
JavaScript is one of the most popular programming languages. Very often you see new JavaScript frameworks that make your life easier as a software developer, but being able to learn the language itself, not just the latest framework, can also be quite helpful.
GitHub is one of the most popular version control repositories. In it, you can find countless public projects in many programming languages.
I have already written an article about DownGit, which gives you the ability to create a download link for GitHub projects. This is a Chrome extension that you can use to add additional functionalities that can be helpful to you from time to time.
Software developers use the words libraries and frameworks very frequently and interchangeably, but they are not the same. Koushik Kothagal, who is a really good programming instructor, with a lot of helpful tutorials, especially for Java technologies, has published a really great video explaining both of these concepts and is really easy to understand and follow.
I recently learned about a new developer related site that I really liked. It is a place where many recordings of conferences about software development have been listed. Continue reading
GitHub is one of the best places for developers to store their projects, collaborate with other colleagues, and learn from other open source projects as well. These type of projects are written in different programming languages, usually using different frameworks. As these frameworks or developers in general usually possess certain ways of organizing their code files into many folders, it makes it a bit difficult to navigate through them manually in GitHub. Luckily there is a really helpful shortcut that help with that. Continue reading
I am an experienced and passionate Senior Software Engineer with a demonstrated history of working in the full life cycle of software development with enormous curiosity for data science, machine learning, algorithms, data structures, and solving challenging problems. I am an open-source enthusiast at https://github.com/fatosmorina and also a writer.
I am open for new opportunities.