{"id":8790,"date":"2023-11-04T11:11:33","date_gmt":"2023-11-04T05:41:33","guid":{"rendered":"https:\/\/python-programs.com\/?p=8790"},"modified":"2023-11-10T12:16:32","modified_gmt":"2023-11-10T06:46:32","slug":"python-program-to-check-if-a-date-is-valid-and-print-the-incremented-date","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-if-a-date-is-valid-and-print-the-incremented-date\/","title":{"rendered":"Python Program to Check if a Date is Valid and Print the Incremented Date"},"content":{"rendered":"
Given a date , the task is to Check if a Date is Valid and increment the given date and print it in python<\/p>\n
Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to check and implement the increment the given date in Python:<\/p>\n Drive into Python Programming Examples<\/a> and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Given a date , the task is to Check if a Date is Valid and increment the given date and print it in python Examples: Example1: Input: given date =”11\/02\/2001″ Output: The incremented given date is: 12 \/ 2 \/ 2001 Example2: Input: given date = “29\/02\/2001” Output: The given date 29\/02\/2001 is not valid …<\/p>\ngiven date =\"11\/02\/2001\"<\/pre>\n
The incremented given date is: 12 \/ 2 \/ 2001<\/pre>\n
given date = \"29\/02\/2001\"<\/pre>\n
The given date 29\/02\/2001 is not valid<\/pre>\n
Program to Check and Print the Incremented Date in Python<\/h2>\n
\n
1)By using if..elif..else Conditional Statements\u00a0 and split() function(Static Input)<\/h3>\n
\n
# given given_data\r\ngiven_date = \"11\/02\/2001\"\r\n# splitting the given_data by \/ character to separate given_data,month and year\r\nday, month, year = given_date.split('\/')\r\nday = int(day)\r\nmonth = int(month)\r\nyear = int(year)\r\nif(month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12):\r\n maxdays = 31\r\nelif(month == 4 or month == 6 or month == 9 or month == 11):\r\n maxdays = 30\r\nelif(year % 4 == 0 and year % 100 != 0 or year % 400 == 0):\r\n maxdays = 29\r\nelse:\r\n maxdays = 28\r\nif(month < 1 or month > 12):\r\n print(\"The given date\", given_date, \"is not valid\")\r\nelif(day < 1 or day > maxdays):\r\n print(\"The given date\", given_date, \"is not valid\")\r\nelif(day == maxdays and month != 12):\r\n day = 1\r\n month = month+1\r\n print(\"The incremented given date is: \", day, month, year)\r\nelif(day == 31 and month == 12):\r\n day = 1\r\n month = 1\r\n year = year+1\r\n print(\"The incremented given date is: \", day, '\/', month, '\/', year)\r\nelse:\r\n day = day + 1\r\n print(\"The incremented given date is: \", day, '\/', month, '\/', year)\r\n<\/pre>\n
The incremented given date is: 12 \/ 2 \/ 2001<\/pre>\n
\n
2)By using if..elif..else Conditional Statements\u00a0 and split() function(User Input)<\/h3>\n
\n
# Scan the date, month and year as int(input()).\r\nday = int(input(\"Enter some random day = \"))\r\nmonth = int(input(\"Enter some random month = \"))\r\nyear = int(input(\"Enter some random year = \"))\r\nif(month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12):\r\n maxdays = 31\r\nelif(month == 4 or month == 6 or month == 9 or month == 11):\r\n maxdays = 30\r\nelif(year % 4 == 0 and year % 100 != 0 or year % 400 == 0):\r\n maxdays = 29\r\nelse:\r\n maxdays = 28\r\nif(month < 1 or month > 12):\r\n print(\"The given date\", given_date, \"is not valid\")\r\nelif(day < 1 or day > maxdays):\r\n print(\"The given date\", given_date, \"is not valid\")\r\nelif(day == maxdays and month != 12):\r\n day = 1\r\n month = month+1\r\n print(\"The incremented given date is: \", day, month, year)\r\nelif(day == 31 and month == 12):\r\n day = 1\r\n month = 1\r\n year = year+1\r\n print(\"The incremented given date is: \", day, '\/', month, '\/', year)\r\nelse:\r\n day = day + 1\r\n print(\"The incremented given date is: \", day, '\/', month, '\/', year)\r\n<\/pre>\n
Enter some random day = 11\r\nEnter some random month = 2\r\nEnter some random year = 2001\r\nThe incremented given date is: 12 \/ 2 \/ 2001<\/pre>\n
\n