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

aspn

I want to find the number of days between two months. I use Xcode but don't want to go through the trouble of installing boost or 'date.h', so I tried to do it more primitively but somehow the code keeps breaking at a certain point:

    for ( it=mymap.begin() ; it != mymap.end(); it++ ) {
        auto nx = next(it);

        if (it->second.patientID == nx->second.patientID) {

            //31 28 31 30 31 30 31 31 30 31 30 31
            yue = it->second.month;
            yue2 = nx->second.month;
            sincejan1 = 0;
            sincejan = 0;

            //it keeps breaking at the line below
            if (abs(yue-yue2) > 0) {

            if (yue ==12)
                sincejan1 = 365-31;
            if (yue ==11)
                sincejan1 = 365-31-30;
            if (yue ==10)
                sincejan1 = 365-31-30-31;
            if (yue ==9)
                sincejan1 = 365-31-30-31-30;
            if (yue ==8)
                sincejan1 = 31+28+31+30+31+30+31+31;
            if (yue ==7)
                sincejan1 = 31+28+31+30+31+30+31;
            if (yue ==6)
                sincejan1 = 31+28+31+30+31+30;
            if (yue ==5)
                sincejan1 = 31+28+31+30+31;
            if (yue ==4)
                sincejan1 = 31+28+31+30;
            if (yue ==3)
                sincejan1 = 31+28+31;
            if (yue ==2)
                sincejan1 = 31+28;
            if (yue ==1)
                sincejan1 = 31;

            if (yue2 ==12)
                sincejan = 365-31;
            if (yue2 ==11)
                sincejan = 365-31-30;
            if (yue2 ==10)
                sincejan = 365-31-30-31;
            if (yue2 ==9)
                sincejan = 365-31-30-31-30;
            if (yue2 ==8)
                sincejan = 31+28+31+30+31+30+31+31;
            if (yue2 ==7)
                sincejan = 31+28+31+30+31+30+31;
            if (yue2 ==6)
                sincejan = 31+28+31+30+31+30;
            if (yue2 ==5)
                sincejan = 31+28+31+30+31;
            if (yue2 ==4)
                sincejan = 31+28+31+30;
            if (yue2 ==3)
                sincejan = 31+28+31;
            if (yue2 ==2)
                sincejan = 31+28;
            if (yue2 ==1)
                sincejan = 31;
            }

            monthDiff = sincejan1 - sincejan;
        }
    }

I'm not sure what's wrong or if this is an okay way to do this. I would appreciate greatly any help/advice! I'm a programming beginner.

selbie

This is similar to an interview question I ask prospective hires to code for me. (And for purposes of interview evaluation, they are not allowed to use the built in date/time functions).

And the OP's problem is simplified to measuring the delta in days of dates within the same year - so that makes things easier.

To start, we need a simple function to tell us if the year we are dealing with is a leap year, because at some point in the code, we'll have to deal with leap years. And, leap year is a bit more than just "every four year". But you already know that.

bool isLeapYear(int year)
{
    bool isDivisibleByFour = !(year % 4);
    bool isDivisibleBy100 = !(year % 100);
    bool isDivisibleBy400 = !(year % 400);
    return (isDivisibleBy400) || (isDivisibleByFour && !isDivisibleBy100);
}

And we'll need another helper function to return the number of days in a month and it needs to account for for leap years in February.

int getDaysInMonth(int month, int year)
{
    int days_in_month[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };

    int result = days_in_month[month-1];
    if ((month == 2) && isLeapYear(year))
    {
        result++;
    }
    return result;
}

Assume in the above code that "January" would be represented by "month == 1" and December is "month == 12". Hence, the [month-1] thing in the array lookup. We would add some parameter validation to the above code, but it should work for purposes of discussion.

Now we have a way to count days in a month, we need to a function that will tell us "how many days since the beginning of the year" for a given month/day/year.

int getDayOfYear(int month, int day, int year)
{
    int count = 0;
    int m = 1;

    while (m != month)
    {
        count += getDaysInMonth(m, year);
        m++;
    }
    count += day - 1;
    return count;
}

The function above will return "0" for (1,1,2015) and "364" for (12,31,2015). Again, parameter validation would be needed for production code.

Now to compute the difference in days between any two days of the same year:

int getDiffOfDaysInSameYear(int month1, int day1, int month2, int day2, int year)
{
    int day_of_year1 = getDayOfYear(month1, day1, year);
    int day_of_year2 = getDayOfYear(month2, day2, year);
    return day_of_year2 - day_of_year1;
}

Let's test it out:

int main()
{
    int x = getDiffOfDaysInSameYear(4,4, 10,24, 2015); // number of days to get to  10/24/2015 from 4/4/2015
    printf("The delta in days between April 4 and October 2015 is: %d days\n", x);
    return 0;
}

Prints out: The delta in days between April 4 and October 2015 is: 203 days

If you want to simplify it to just counting days between months, then just pass in "1" for the days.

int main()
{
    int x = getDiffOfDaysInSameYear(5, 1, 11, 1, 2015);
    printf("The delta in days between May and November is %d\n", x);
    return 0;
}

Prints out: The delta in days between May and November is 184

Hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Finding the number of days between two dates

From Dev

Year, Month, Days, hours between two dates

From Dev

Calculate days between two dates ignoring year

From Java

Calculate difference between two dates (number of days)?

From Dev

Number of days between two dates in Thymeleaf

From Dev

Calculate number of days between two dates?

From Dev

awk to calculate number of days between two dates:

From Dev

Calculate number of days between two dates with momentsjs

From Dev

Calculate number of days between two dates in r

From Dev

Getting the number of days between two dates

From Dev

AngularJS - Javascript number of days between two dates

From Dev

Number of days between two dates in JavaScript

From Dev

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

From Dev

Add number of days to datetime and then find difference in days between two dates

From Dev

find the number of days between dates C#

From Dev

Get the number of days between two dates in Oracle, inclusive of the dates

From Dev

Number of days between dates?

From Dev

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

From Dev

Find number of days, weeks, months and year between two milliseconds.

From Dev

And again about calculate the number of days between two dates using JavaScript

From Dev

How can I calculate the number of working days between two dates

From Java

How to calculate number of days between two given dates?

From Dev

How to find out the number of days between two dates

From Dev

Getting the number of days excluding years between two dates

From Dev

Validating the number of days between two dates in vb.net

From Dev

Calculate the number of business days between two dates in power pivot

From Dev

Counting the number of days excluding sunday between two dates

From Dev

Shopify (liquid): Find number of days between two dates

From Dev

How to calculate the number of days between two given dates

Related Related

  1. 1

    Finding the number of days between two dates

  2. 2

    Year, Month, Days, hours between two dates

  3. 3

    Calculate days between two dates ignoring year

  4. 4

    Calculate difference between two dates (number of days)?

  5. 5

    Number of days between two dates in Thymeleaf

  6. 6

    Calculate number of days between two dates?

  7. 7

    awk to calculate number of days between two dates:

  8. 8

    Calculate number of days between two dates with momentsjs

  9. 9

    Calculate number of days between two dates in r

  10. 10

    Getting the number of days between two dates

  11. 11

    AngularJS - Javascript number of days between two dates

  12. 12

    Number of days between two dates in JavaScript

  13. 13

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

  14. 14

    Add number of days to datetime and then find difference in days between two dates

  15. 15

    find the number of days between dates C#

  16. 16

    Get the number of days between two dates in Oracle, inclusive of the dates

  17. 17

    Number of days between dates?

  18. 18

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

  19. 19

    Find number of days, weeks, months and year between two milliseconds.

  20. 20

    And again about calculate the number of days between two dates using JavaScript

  21. 21

    How can I calculate the number of working days between two dates

  22. 22

    How to calculate number of days between two given dates?

  23. 23

    How to find out the number of days between two dates

  24. 24

    Getting the number of days excluding years between two dates

  25. 25

    Validating the number of days between two dates in vb.net

  26. 26

    Calculate the number of business days between two dates in power pivot

  27. 27

    Counting the number of days excluding sunday between two dates

  28. 28

    Shopify (liquid): Find number of days between two dates

  29. 29

    How to calculate the number of days between two given dates

HotTag

Archive