Convert Days Since 1990 to datetime in Python (2 Examples)
In this article, I’ll demonstrate how to transform days since 1990 to datetime in the Python programming language.
Table of contents:
So let’s just jump right in!
Example Data & Imported Modules
Firstly, we need to import datetime and timedelta from the datetime module and relativedelta from the dateutil module.
from datetime import datetime, timedelta # Loading the modules from datetime module from dateutil.relativedelta import relativedelta # Loading relativedelta from dateutil module
Next, we’ll create data that we can use in the following examples:
day_amount = 6500 # Sample day data construction start_datetime = datetime(1990,1,1,0,0,0) # Sample start datetime construction print(start_datetime) # Printing sample datetime # 1990-01-01 00:00:00
Example 1: Using timedelta() for Adding Days
This example shows how to utilize timedelta() for generating an end date as a datetime object.
print(start_datetime + timedelta(days = day_amount)) # Generating and printing the end datetime # 2007-10-19 00:00:00
Example 2: Using relativedelta() for Adding Days
In this example, I’ll demonstrate how to use relativedelta() to add days to the initial datetime object.
print(start_datetime + relativedelta(days = day_amount)) # Generating and printing the end datetime # 2007-10-19 00:00:00
Video, Further Resources & Summary
I have recently released a video on my YouTube channel, which shows the Python programming code of this article. You can find the video below:
The YouTube video will be added soon.
In addition, you might want to have a look at the related tutorials on this website. You can find some articles on topics such as time objects, data conversion, and character strings below:
- Dates & Times in Python – datetime Module
- Introduction to timedelta Objects in Python
- Convert Unix Timestamp to datetime in Python
- Convert datetime to Unix Timestamp in Python
- Convert datetime to Different Time Zone in Python
- Convert datetime to String without Microsecond Component in Python
- Introduction to Python
This tutorial has explained how to generate the end datetime with days since 1990 in Python programming. Please let me know in the comments section, in case you have further questions.
This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get further information about his professional background, a list of all his tutorials, as well as an overview on his other tasks on Statistics Globe.