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:

 

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.

 

Ömer Ekiz Python Programming & Informatics

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top