Find out the future date with Python

This tutorial will focus on how to add days to date in Python or you can say how to find a future date in Python.

Hey Pythoneers,

Do you sometimes get lost and wonder finding out the dates? Do you ever rush to the calendar for it?

What will be the date after 75 days from today?  To find out you have two options:

  • Either open your calendar and count 75 days (too much time taking process) OR
  • Follow this simple program to find out the date in seconds.

In this tutorial we’ll learn how to calculate the future date with some inputs. You have to enter the start date (day,month and the year) and the no. of incremental days.

Find out a future date in Python

import array
month=array.array('i',[0,31,28,31,30,31,30,31,31,30,31,30,31])
count=0
d=int(input("Enter a day:"))
months=int(input("Enter a month:"))
year=int(input("Enter a year:"))
days=int(input("Enter number of days:"))
if year%4==0:
    month[2]=28
while count<days:
    d=d+1
    count=count+1
    if d>month[months]:
        d=1
        months=months+1
    if months>12:
        months=1
        year=year+1
        if year%4==0:
            month[2]=29
        else:
            month[2]=28

print("Future date= ", end='')
print(d,end='-')
print(months,end='-')
print(year)

Supposedly, you enter 25-5-2019 as the start date and you want the future date after 75 days then the code will produce the following output:

Enter a day:25
Enter a month:5
Enter a year:2019
Enter the number of days to find out the future date:75
Future date= 8-8-2019

Therefore, the future date of 25-5-2019 after 75 days is  8-8-2019.

Note: This code works with any date but the inputs should be taken in integer values only.

So, here it is. This Python program saves your time and the irritation of repeatedly opening the calendar and counting the days. Remember that array should be used to store the number of days each month contains.

You may also read,

Leave a Reply

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