Date Questions Universal Template

//Judge the validity of the date
int check_valid(int date) 
{     int year = date / 10000;     int month = date % 10000 / 100;     int day = date % 100;


   if (month <= 0 || month >= 13) return 0;
    // There are twelve months in a year, and February is special, so let's discuss it separately.
    if (day == 0 || month != 2 && day > months[month]) return 0;
    if (month == 2)
    {         int leap = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);//Judge whether it is a leap year (February 29 days in leap years, February 28 days in ordinary years)         if (day > 28 + leap) return 0;//Maximum 29 days in February     }     return 1;    




//The number of days corresponding to each of the twelve months (because the array starts from 0, and our month starts from January, so the length of the set array is 13)

int months[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

//Get the number of days in a certain month
int get(int year, int month)
{     if (month != 2) return months[month];     else     {         // February         int leap = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);         return 28 + leap;         }    






}

The specific code and case are as follows:

 I hope that after reading this template, you can deal with dates and birthdays proficiently.

Guess you like

Origin blog.csdn.net/m0_67843030/article/details/130019825