Determining the difference between dates

后端 未结 8 1871
無奈伤痛
無奈伤痛 2020-12-10 04:34

I\'m trying to figure out a way for my program to take a date (like February 2nd, 2003) and show the difference between the two with another date (like April 2nd, 2012), exc

相关标签:
8条回答
  • 2020-12-10 05:11

    There is another way round...

    • Given two dates, take the year of the earlier date as the reference year.
    • Then calculate no. of days between each of the two given dates and that 1/1/<that year>
    • Keep a separate function that tells the number of days elapsed till a specific month.
    • The absolute difference of those two no. of days will give the difference between the two given dates.
    • Also, do not forget to consider leap years!

    The code:

    #‎include‬<stdio.h>
    #include<math.h>
    typedef struct
    {
        int d, m, y;
    } Date;
    int isLeap (int y)
    {
        return (y % 4 == 0) && ( y % 100 != 0) || (y % 400 == 0);
    }
    int diff (Date d1, Date d2)                         //logic here!
    {
        int dd1 = 0, dd2 = 0, y, yref;                  //dd1 and dd2 store the <i>no. of days</i> between d1, d2 and the reference year
        yref = (d1.y < d2.y)? d1.y: d2.y;               //that <b>reference year</b>
        for (y = yref; y < d1.y; y++)
            if (isLeap(y))                              //check if there is any leap year between the reference year and d1's year (exclusive)
                dd1++;
        if (isLeap(d1.y) && d1.m > 2) dd1++;                //add another day if the date is past a leap year's February
        dd1 += daysTill(d1.m) + d1.d + (d1.y - yref) * 365;     //sum up all the tiny bits (days)
        for (y = yref; y < d2.y; y++)                       //repeat for d2
            if(isLeap(y))
                dd2++;
        if (isLeap(y) && d2.m > 2) dd2++;
        dd2 += daysTill(d2.m) + d2.d + (d2.y - yref) * 365;
        return abs(dd2 - dd1);                          //return the absolute difference between the two <i>no. of days elapsed past the reference year</i>
    }
    int daysTill (int month)                            //some logic here too!!
    {
        int days = 0;
        switch (month)
        {
            case 1: days = 0;
            break;
            case 2: days = 31;
            break;
            case 3: days = 59;
            break;
            case 4: days = 90;      //number of days elapsed before April in a non-leap year
            break;
            case 5: days = 120;
            break;
            case 6: days = 151;
            break;
            case 7: days = 181;
            break;
            case 8: days = 212;
            break;
            case 9: days = 243;
            break;
            case 10:days = 273;
            break;
            case 11:days = 304;
            break;
            case 12:days = 334;
            break;
        }
        return days;
    }
    main()
    {
        int t;          //no. of test cases
        Date d1, d2;    //d1 is the first date, d2 is the second one! obvious, duh!?
        scanf ("%d", &t);
        while (t--)
        {
            scanf ("%d %d %d", &d1.d, &d1.m, &d1.y);
            scanf ("%d %d %d", &d2.d, &d2.m, &d2.y);
            printf ("%d\n", diff(d1, d2));
        }
    }
    

    Standard Input:

    1
    23 9 1960
    11 3 2015
    

    Standard Output:

    19892
    

    Code in action: https://ideone.com/RrADFR

    Better algorithms, optimizations and edits are always welcome!

    0 讨论(0)
  • 2020-12-10 05:14

    I'm not sure what platform are you on? Windows, Linux? But let us pretend that you would like to have a platform independent solution and the langugage is standard C++.

    If you can use libraries you can use the Boost::Date_Time library (http://www.boost.org/doc/libs/1_49_0/doc/html/date_time.html)

    If you cannot use libraries to solve your assignment, you will need to find a common simple ground. Maybe you could convert all the dates to seconds, or days substract them and then convert that back to the data again. Substracting days or months as integers will not help as it will lead to incorrect results unless you do not take into account the rest. Hope that helps.

    Like dbrank0 pointed it out. :)

    0 讨论(0)
  • 2020-12-10 05:21

    You should look at the DateTime class.

    Also the msdn reference for C++ syntax.

    0 讨论(0)
  • 2020-12-10 05:28

    If you need to do it yourself, then one way to do this pretty easy is by converting dates into a Julian Day. You get formulas at that link, and from conversion on, you only work with floats, where each day is 1 unit.

    0 讨论(0)
  • 2020-12-10 05:29

    Here is a complete code to calculating date difference in y/m/d.

    Assuming that to and from are date types, and that months and days start from 1 (similar to Qt):

    static int increment[12] = { 1, -2, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 };
    
    int daysInc = 0;
    if (to.day() - from.day() < 0)
    {
        int month = to.month() - 2; // -1 from zero, -1 previous month.
        if (month < 0)
            month = 11; // Previous month is December.
        daysInc = increment[month];
        if ( (month == 1) && (to.year()%4 == 0) )
            daysInc++; // Increment days for leap year.
    }
    
    int total1 = from.year()*360 + from.month()*30 + from.day();
    int total2 = to.year()*360 + to.month()*30 + to.day();
    int diff = total2 - total1;
    int years = diff/360;
    int months = (diff - years*360)/30;
    int days = diff - years*360 - months*30 + daysInc;
    
    // Extra calculation when we can pass one month instead of 30 days.
    if (from.day() == 1 && to.day() == 31) {
        months--;
        days = 30;
    }
    

    I tried this algorithm and it is working okay. Let me know if you have troubles using/understanding it.

    0 讨论(0)
  • 2020-12-10 05:32

    Since you are looking for mathematical formula , it will help you to find a solution to your problem. Let Y be the year,M be the month and D be the day. Do this calculation for both the dates.

    Total = Y* 365 + M*30 + D ,then find the difference between 2 totals of the corresponding dates.

    While multiplying 30 with M value ,you have to give the number of days in that month. You can do it with #define value or if loop. Similarly you can do for leap year too by multiplying 366 with Y .

    Hope this will help u....

    0 讨论(0)
提交回复
热议问题