Total days between 2 dates C++

Joseph

I am trying to solve a problem that asks me to give the total days between two dates.

I have to take care of some issues between those two dates, such as leap years and the way of inputting the years by the users. (For example, if you input 1 and 17, the code will still give you the difference is 16 years (2017 - 2001 = 16). I am not supposed to change ANYTHING inside the main() function.

Here is my code.

 #include <iostream>
 #include <cmath>

 using namespace std;

 class date
 {
    private:
    int m;
    int d;
    int y;

    public:
    date(int, int, int);
    int countLeapYears(date&);
    int getDifference(date&);
    int operator-(date&);    
  };

   int main()
   {
    int day, month, year;
    char c;

    cout << "Enter a start date: " << endl;
    cin >> month >> c >> day >> c >> year;

    date start = date(month, day, year);

    cout << "Enter an end date: " << endl;
    cin >> month >> c >> day >> c >> year;

    date end = date(month, day, year);
    int duration = end-start;

     cout << "The number of days between those two dates are: " <<    
     duration << endl;

     return 0;
   }


    date::date(int a, int b, int c)
    {
    m = a;
    d = b;
    y = c;
    }

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

   int date::countLeapYears(date& d)
   {
      int years = d.y;
      if (d.m <= 2)
      years--;
      return years / 4 - years / 100 + years / 400;
    }

   int date::getDifference(date& other)
   {

      int n1 = other.y*365 + other.d;

      for (int i=0; i<other.m - 1; i++)
     {
       n1 += monthDays[i];
       n1 += countLeapYears(other);
      }

      return n1;
      }

   int date::operator-(date& d) {
      int difference = getDifference(d);
      return difference;
    }

When I ran this code, it said invalid binary operation between "date" and "date".

Now, I assume that when I initialized int duration = end - start, I should have gotten a number. I guess what I am doing wrong here is I failed to convert the (end - start) date type into an integer. I thought my function getDifference already took care of that issue, but apparently it did not.

Howard Hinnant

Challenge accepted.

Using this free, open-source, header-only date library:

#include "date.h"
#include <iostream>

namespace me
{

class date
{
    ::date::sys_days tp_;
public:
    date(int month, int day, int year)
        : tp_{::date::year(year)/month/day}
        {}

    friend
    int
    operator-(const date& x, const date& y)
    {
        return (x.tp_ - y.tp_).count();
    }
};

}  // namespace me

using namespace std;
#define date me::date

int main()...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C# - Get days between 2 dates

From Dev

Total days between two dates by Month

From Dev

Difference between 2 dates in days

From Dev

Get the days of year and all the hours between 2 dates in C#

From Dev

Calculate total business working days between two dates

From Dev

Calculate total business working days between two dates

From Dev

Count number of days between 2 dates in JPA

From Dev

How to count fractional days between 2 dates

From Dev

calculate number of days between 2 dates in php

From Dev

Case with dates between now and 2 days?

From Dev

find the number of days between dates C#

From Dev

Total number of months (Count) between 2 dates

From Dev

Number of days between dates?

From Dev

Days between dates calculation

From Dev

Get total hours worked between 2 days mysql

From Dev

Python How to calculate number of days between 2 dates?

From Dev

Average Number of days between 2 dates in SQL Alchemy with Postgres

From Dev

Generating all days between 2 given dates in Java

From Dev

JS - Calculate number of days between 2 dates considering leap year

From Dev

Number of working days between 2 dates according JIRA's configuration

From Dev

Wrong count of difference days between 2 dates with joda time?

From Dev

Calculate number of days between 2 dates, then sum and group by month

From Dev

javascript how to check number of days in between 2 dates

From Dev

Calculating days/months/years between 2 dates in Drools

From Dev

How do I grab total number of days selected between two dates?

From Dev

finding number of days between two dates in the same year (c++)

From Dev

Business days between two dates in objective-c?

From Dev

SQL total days between fields

From Dev

Total days from two dates in mysql

Related Related

  1. 1

    C# - Get days between 2 dates

  2. 2

    Total days between two dates by Month

  3. 3

    Difference between 2 dates in days

  4. 4

    Get the days of year and all the hours between 2 dates in C#

  5. 5

    Calculate total business working days between two dates

  6. 6

    Calculate total business working days between two dates

  7. 7

    Count number of days between 2 dates in JPA

  8. 8

    How to count fractional days between 2 dates

  9. 9

    calculate number of days between 2 dates in php

  10. 10

    Case with dates between now and 2 days?

  11. 11

    find the number of days between dates C#

  12. 12

    Total number of months (Count) between 2 dates

  13. 13

    Number of days between dates?

  14. 14

    Days between dates calculation

  15. 15

    Get total hours worked between 2 days mysql

  16. 16

    Python How to calculate number of days between 2 dates?

  17. 17

    Average Number of days between 2 dates in SQL Alchemy with Postgres

  18. 18

    Generating all days between 2 given dates in Java

  19. 19

    JS - Calculate number of days between 2 dates considering leap year

  20. 20

    Number of working days between 2 dates according JIRA's configuration

  21. 21

    Wrong count of difference days between 2 dates with joda time?

  22. 22

    Calculate number of days between 2 dates, then sum and group by month

  23. 23

    javascript how to check number of days in between 2 dates

  24. 24

    Calculating days/months/years between 2 dates in Drools

  25. 25

    How do I grab total number of days selected between two dates?

  26. 26

    finding number of days between two dates in the same year (c++)

  27. 27

    Business days between two dates in objective-c?

  28. 28

    SQL total days between fields

  29. 29

    Total days from two dates in mysql

HotTag

Archive