In this article, you will learn how to find the number of days between two dates. That means, if the given two dates are dt1 and dt2, you need to find the total number of days between them.
For example,
Input: dt1 = {10, 2, 2022} dt2 = {8, 1, 2023} Output: Difference between two dates is 332
Disclaimer: In my code date is only valid if lying in the range of 1800 to 9999. You can change it as per your requirement.
Approach (Using Subtraction):
Follow the steps below to solve the problem: But you should try the problem yourself first.
- First, validate the entered date. Because it is a primary prerequisite.
- Calculate the total number of days in the given first date (dt1).
- Add extra days for dt1 if it has leap years.
- Calculate the total number of days in the given first date (dt2).
- Add extra days for dt2 if it has leap years.
- Now just subtract dt1 and dt2 to get the total number of days between the dates.
C Program to find the number of days between two given dates:
The following is a C program to calculate the difference between two dates in days. You must need to enter the date in the format of mentioned structure (SDate).
#include <stdio.h> #define MAX_YR 9999 #define MIN_YR 1800 #define INVALID_YEAR -1 #define INVALID_DAYS_CNT -1 //structure for day, month, year typedef struct { int dd; int mm; int yyyy; } SDate; // 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 validDate(const SDate *const pValidDate) { //check range of year,month and day if (pValidDate->yyyy > MAX_YR || pValidDate->yyyy < MIN_YR) return 0; if (pValidDate->mm < 1 || pValidDate->mm > 12) return 0; if (pValidDate->dd < 1 || pValidDate->dd > 31) return 0; //Handle feb days in leap year if (pValidDate->mm == 2) { if (isLeapYear(pValidDate->yyyy)) return (pValidDate->dd <= 29); else return (pValidDate->dd <= 28); } //handle months which has only 30 days if (pValidDate->mm == 4 || pValidDate->mm == 6 || pValidDate->mm == 9 || pValidDate->mm == 11) return (pValidDate->dd <= 30); return 1; } //number of days in month const int monthDays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // This function counts number of // leap years before the given date int countLeapYears(const SDate * const pDate) { int cntLeapYear = INVALID_YEAR; if(pDate != NULL) { int years = pDate->yyyy; //If current is not aplicable for leap // year then exclude it. if (pDate->mm <= 2) years--; // A year is a leap year if it // is a multiple of 4, // multiple of 400 and not a // multiple of 100. cntLeapYear = ((years / 4) - (years / 100) + (years / 400)); } return cntLeapYear; } // Function returns number of // days between two given dates int daysBetweenDates(const SDate *const pDt1, const SDate * const pDt2) { if((pDt1 == NULL) && (pDt2 == NULL)) { return -1; } int daysCnt = INVALID_DAYS_CNT; int isDateValid = validDate(pDt1); isDateValid= isDateValid? validDate(pDt2):0; if(isDateValid) { // COUNT TOTAL NUMBER OF DAYS // BEFORE FIRST DATE 'pDt1' // initialize count using years and day long int dayCnt1 = (pDt1->yyyy * 365) + pDt1->dd; int index = 0; // Add days for months in given date for (index = 0; index < (pDt1->mm - 1); ++index) { dayCnt1 += monthDays[index]; } // Add a day extra day for every leap year // beacause leap year has 366 days, dayCnt1 += countLeapYears(pDt1); // SIMILARLY, COUNT TOTAL NUMBER OF // DAYS BEFORE 'pDt2' long int dayCnt2 = (pDt2->yyyy * 365) + pDt2->dd; for (index = 0; index < (pDt2->mm - 1); ++index) { dayCnt2 += monthDays[index]; } //Add extra day of leap years dayCnt2 += countLeapYears(pDt2); //number of days between two date daysCnt = (dayCnt2>=dayCnt1)? (dayCnt2 - dayCnt1):(dayCnt1-dayCnt2); } else { printf("Date is invalid.\n"); } return daysCnt; } // Driver code int main() { SDate dt1 = {10, 2, 2022}; SDate dt2 = {8, 1, 2023}; //calling function const int dayCntBetweenDays = daysBetweenDates(&dt1,&dt2); if(dayCntBetweenDays != INVALID_DAYS_CNT) { printf( "Difference between two dates is %d\n",dayCntBetweenDays); } return 0; }
Output:
Difference between two dates is 332
Recommended Post:
- Check date validity in C?
- C program to find number of days in a month.
- C Programming Courses And Tutorials.
- CPP Programming Courses And Tutorials.
- Python Courses and Tutorials.
- Count of Leap Years in a given year range.
- Find the prime number using the C program.
- Using the C program to check valid date (date is valid or not)
- Check expiry date Using C program
- C program to print day name of week
- Convert number of days in terms of Years, Weeks and Days using C program
- C program to find century for a year