Find number of days between two dates in C++

In this article, let us discuss an efficient method to calculate the number of days between any two dates in C++. Here we are going to consider the Gregorian calendar, which has 12 months and 365 days.

Note: We also have 366 days in some years which are known as leap years.

We will do this using two different methods:

  1. Using math.h
  2. Using Chrono library

In this article, we are going to consider DD/MM/YYYY format for dates.

Count the days between two dates using math.h

  • In the brute-force approach, we can simply count the days from date1 to date2.
  • In an efficient approach, we can find the number of days from 00/00/0000 to date1 and we also find the number of days from 00/00/0000 to date2 and we return the difference between the both as an output.

Let us discuss an efficient approach along with some code.

Steps : 

Initially consider two dates in { dd, mm, yyyy } format using arrays. Let’s name them date1 and date2.

We can get the count of days from 00/00/0000 to date1 by adding the following:

  • the number of days from oooo year to date1[2] year
     c1 = (date1[2] * 365);
  •  Some of the years may be leap years so, they may have an extra 1 day. So we need to add the number of leap year days count to c1. We can get the number of leap year days by the following logic.
    ((year/4) + (year/400) - (year/100 ))
  • We have to add the number of days from January until the current month(not including the current month).In the code, we have done this using the function noOfMonthsDays(date). For this, we have to consider a constant array that stores the number of days of each month.
  • We have to add the number of days of the current month. We can do this by adding date1[0].

Similarly, calculate for date2 also, Then find the difference between the two counts c1 and c2 and return the answer ,like in below code.

Code : 

#include <iostream>
#include <math.h>
using namespace std;

const int Days_month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,31 };

int noOfLeapYearDays(int date[])
{
    int year = date[2];
    // if month is january or february then we cannot consider this year
    if(date[1] <= 2)
    {
        year = year-1;
    }
    return ((year/4) + (year/400) - (year/100));
}
int noOfMonthsDays(int date[])
{
    int c=0;
    for(int i=0; i < date[1]-1; i++)
    {
        c += Days_month[i];
    }
    return c;
}
long long numberOfDays(int date1[],int date2[])
{
    long long c1,c2;
    c1 = (date1[2] * 365);
    c1 += noOfMonthsDays( date1 ) + date1[0] + noOfLeapYearDays(date1);
    c2 = (date2[2] * 365);
    c2 += noOfMonthsDays( date2 ) + date2[0] + noOfLeapYearDays(date2);
    return (c2-c1);
    
}
int main()
{
    int date1[3] = {26, 1, 2002};
    int date2[3] = {22, 12, 2022};
    cout<<"Number of days betweeen "<< date1[0] << "/" << date1[1] << "/" << date1[2] << " and " <<  date2[0] << "/" << date2[1] << "/" << date2[2] << " is : " << numberOfDays(date1,date2);
    return 0;
}

Output : 

Number of days betweeen 26/1/2002 and 22/12/2022 is : 7635

Find the days between two dates using C++ Chrono library

To find the number of days between two dates in C++, you can use the std::chrono library, which provides a set of types to represent and manipulate dates and times.

Here is an example of how you can use std::chrono to find the number of days between two dates:

#include <iostream>
#include <chrono>

int main() {
  // Define the start and end dates
  std::chrono::system_clock::time_point start_date = std::chrono::system_clock::from_iso_string("2022-01-01");
  std::chrono::system_clock::time_point end_date = std::chrono::system_clock::from_iso_string("2022-01-31");

  // Calculate the number of days between the start and end dates
  std::chrono::duration<int, std::ratio<86400>> diff = end_date - start_date;

  std::cout << "Number of days between the two dates: " << diff.count() << std::endl;

  return 0;
}

The std::chrono::duration type represents a duration of time, and the count() function returns the number of ticks in the duration. In this example, the duration is represented in terms of days, so the count() function will return the number of days between the two dates. Note that this example uses the std::chrono::system_clock type, which represents the system-wide real-time clock. This means that the start and end dates must be specified in UTC. You can use the std::chrono::time_point_cast function to convert the time points to a different time zone if needed.

Leave a Reply

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