Python Program to Print the Incremented Date if valid Last Updated : 16 Feb, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will write a python program to input a date and check whether it is a valid date or not. If it is valid, output the incremented date. Otherwise, print “Invalid date”. Examples: Input : 1/2/2000 Output: 2/2/2000 Input : 29/2/2002 Output: Invalid date Input : 31/12/2015 Output: 1/1/2016 The first step is to check whether the entered date is valid or not. For this step, we need to first obtain the maximum possible day number for the month entered. Then we need to see whether the day lies between 1 and the obtained maximum day number and whether the month lies between 1 and 12. If both these conditions get satisfied, that means it is a valid date, and we need to increment it. To increment the date, we need to handle the following cases: If the entered date is the last day of the year.If the entered date is the last day of the month.If the entered date is not the last day of the month. For the first case, increment the year and set both the day and the month to 1. For the second case, increment the month and set the day to 1. For the third case, just increment the day. If even one of the two conditions did not get satisfied, then it is an invalid date. Below is the implementation. Python3 # input the date and split it to day, month and year day, month, year = map(int, input().split('/')) if month == 2: # check for February if year % 4 != 0: d_max = 28 else: d_max = 29 elif month in [4, 6, 9, 11]: # check the months with 30 days d_max = 30 else: d_max = 31 if 1 <= day <= d_max and 1 <= month <= 12: # increment the date since it is a # valid date if day == d_max: day = 1 if month == 12: # If this block is entered, # then it is the last day of # the year month = 1 year += 1 else: # If this block is entered, # then it is the last day of # the month month += 1 else: # If this block is entered, then it # is NOT the last day of the month day += 1 print(str(day) + "/" + str(month) + "/" + str(year)) else: print("Invalid date") Output: 31/12/2015 1/1/2016 Time Complexity: O(n)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Python Program to Display Calendar of a Given Month S shubhi18195 Follow Improve Article Tags : Python Python Programs Python datetime-program Practice Tags : python Similar Reads Python program to print current year, month and day In this article, the task is to write a Python Program to print the current year, month, and day. Approach: In Python, in order to print the current date consisting of a year, month, and day, it has a module named datetime. From the DateTime module, import date classCreate an object of the date clas 1 min read Python Program to check date in date range Given a date list and date range, the task is to write a Python program to check whether any date exists in the list in a given range. Example: Input : test_list = [datetime(2019, 12, 30), datetime(2018, 4, 4), datetime(2016, 12, 21), datetime(2021, 2, 2), datetime(2020, 2, 3), datetime(2017, 1, 1)] 10 min read Python program to print Calendar without calendar or datetime module Given the month and year. The task is to show the calendar of that month and in the given year without using any module or pre-defined functions. Examples: Input : mm(1-12) :9 yy :2010 Output : September 2010 Su Mo Tu We Th Fr Sa 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2 3 min read Python Program to Display Calendar of a Given Month Displaying a calendar for a given month is a common task in programming, and Python provides several methods to achieve this. In this article, we will explore different approaches to creating a Python program that displays the calendar of a specified month. Whether using built-in libraries or third- 4 min read Python program to find number of days between two given dates Finding the number of days between two dates means calculating how much time has passed from one date to another. This is helpful in real-life situations like tracking deadlines, calculating age, planning events or measuring durations in billing and scheduling.Example: Input: dt1 = 01/01/2004, dt2 = 3 min read Python - Validate String date format Given a date format and a string date, the task is to write a python program to check if the date is valid and matches the format.Examples:Input : test_str = '04-01-1997', format = "%d-%m-%Y"Output : TrueExplanation : Formats match with date.Input : test_str = '04-14-1997', format = "%d-%m-%Y"Output 3 min read Like