C program to check valid date (date is valid or not)

check date validity

There are a lot of people who don’t know how to check the valid date and make mistakes at the time of the creation of the date. In this article, I will describe the way to how to create and check the validity of the date.

Whenever you require to create a date manually then you should care about the following points.

  • Year, months and days range.
  • Leap Year.
  • Months that have 30 days
  • Months that have 31 days

 

If you want to learn more about the c language, here 10 Free days C video course for you.

C tutorial

 

In the below program, I am reading the user entered data and create date in dd/mm/yyyy format. Before creating the date I will verify the user entered data.

  • I am following the below steps to accomplish the program.
  • First checking range of the years, months and days.
  • Handle the leap year (day of Feb months).
  • Handle the months which have 30 days.

See below program to check valid date:

This function read the date in the format of dd/mm/yyyy, so enter the data in the mentioned format. If you want you can change the code and take the user input one by one.

 

#include <stdio.h>
#include <time.h>

#define MAX_YR  9999
#define MIN_YR  1900


//structure to store date
typedef struct
{
    int yyyy;
    int mm;
    int dd;
} Date;


// Function to check leap year.
//Function returns 1 if leap year
int  IsLeapYear(int year)
{
    return (((year % 4 == 0) &&
             (year % 100 != 0)) ||
            (year % 400 == 0));
}

// returns 1 if given date is valid.
int isValidDate(Date *validDate)
{
    //check range of year,month and day
    if (validDate->yyyy > MAX_YR ||
            validDate->yyyy < MIN_YR)
        return 0;
    if (validDate->mm < 1 || validDate->mm > 12)
        return 0;
    if (validDate->dd < 1 || validDate->dd > 31)
        return 0;

    //Handle feb days in leap year
    if (validDate->mm == 2)
    {
        if (IsLeapYear(validDate->yyyy))
            return (validDate->dd <= 29);
        else
            return (validDate->dd <= 28);
    }

    //handle months which has only 30 days
    if (validDate->mm == 4 || validDate->mm == 6 ||
            validDate->mm == 9 || validDate->mm == 11)
        return (validDate->dd <= 30);

    return 1;
}



int main(void)
{

    Date getDate = {0}; //variable to store date
    int status = 0; //variable to check status

    //get date year,month and day from user
    printf("\n\n Enter date in format (day/month/year): ");
    scanf("%d/%d/%d",&getDate.dd,&getDate.mm,&getDate.yyyy);

    //check date validity
    status = isValidDate(&getDate);
    if(status !=1)
    {
        printf("\n\n Please enter a valid date!\n");
    }
    else
    {
        printf("\n\n Date is valid!\n");
    }

    return 0;
}

 

check valid date

check valid date

 

Code Analysis:

In the above c code example, first, we ask the users to enter the date in the format of years, months and days.

//get date year,month and day from user
printf("\n\n Enter date in format (day/month/year): ");
scanf("%d/%d/%d",&getDate.dd,&getDate.mm,&getDate.yyyy);

 

 

Now check the validity of the date to call the function is ValidDate. In which I am checking the validity of the days months and years as per the calendar. if the date is valid, displaying the message valid date.

//check date validity
status = isValidDate(&getDate);
if(status !=1)
{
    printf("\n\n Please enter a valid date!\n");
}
else
{
    printf("\n\n Date is valid!\n");
}

 

Recommended Articles for you:

 



Leave a Reply

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