How to check whether date is valid or not in C++

This tutorial demonstrates Program to check whether date is valid or not in C++. The user will enter the date he/she requires to check and output will show the result.

Introduction

For a date to be valid is that it should occur somewhere time. For the validation of date, we check Day Month Year.

Firstly, we check the year. The valid range set in the program is between 1000 and 3000. If it is not in range then the date is invalid.

Secondly, the validation of month is done it has to be between 1 to 12.

Finally, the days are checked according to months according. The value of days can range between 1 to 28,29,30,31.

Example: Input: 12 02 1999

output: The string is valid.

Demonstration of Date checking

The user enters the date to be check. Value is inside variables day, month, year.

The year is check if it is between 1000 to 3000.

If the year is valid then the month is check, the month has to be between 1 to 12.

Finally, the day is check. The days are checked as per the months January, March, May, July, August, October, December will have 31 days and all the rest will have 30 days excluding February. For February we have to check the year first if it is multiple of 4 then the month will have 29 days or else it will have 28 days.

If the date passes all the above cases then it is valid.

Program to check whether date is valid or not in C++

#include<iostream>
using namespace std;

int main(){
  
  int day,month,year;
  
  cout<<"Enter the date (Day Month Year) : \n";
  
  cin>>day>>month>>year;
  
  if(1000 <= year <= 3000)
       {
         if((month==1 || month==3 || month==5|| month==7|| month==8||month==10||month==12) && day>0 && day<=31)
         cout<<"It is valid";
         else 
     if(month==4 || month==6 || month==9|| month==11 && day>0 && day<=30)
            cout<<"It is Valid";
         else
            if(month==2)
               {
               if((year%400==0 || (year%100!=0 && year%4==0)) && day>0 && day<=29)
                 cout<<"It is Valide";
               else if(day>0 && day<=28)
                  cout<<"It is Valid";
               else
                  cout<<"It is Invalid";
               }
         else
               cout<<"It is Invalid";
      }
    else
        cout<<"It is Invalid";
  
  return 0;
}

Input:

12 02 1999

Output:

Enter the date (Day Month Year) :
12 02 1999
It is Valid

Conclusion of this date validation program in C++

The above shown is the program to check whether date is valid or not. We can use the same concept to create many more programs like a Program to check for a particular date lies between the entered two dates.

Also Checkout:

Find difference between two dates in C++

3 responses to “How to check whether date is valid or not in C++”

  1. Bongoman From The Jungle says:

    Hi,

    When testing the code I found 31 3 2020 was accepted as being valid

    line 17 needs to include additional set of parentheses:

    if((month==4 || month==6 || month==9|| month==11) && day>0 && day<=30)

    Thanks for the tutorial.

  2. Bongoman From The Jungle says:

    Sorry, I had typo in the previous message the date I was checking was 31 4 2020 and 31 9 2020.

    Hi, When testing the code I found 31 9 2020 was accepted as being valid line 17 needs to include additional set of parentheses: if((month==4 || month==6 || month==9|| month==11) && day>0 && day<=30) Thanks for the tutorial.

  3. Bongoman From The Jungle says:

    Hi,
    Also line 12 if(1000 <= year = 1000 && year <= 3000)

    Thanks

Leave a Reply

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