Determining the difference between dates

后端 未结 8 1884
無奈伤痛
無奈伤痛 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/
    • 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‬
    #include
    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 no. of days between d1, d2 and the reference year
        yref = (d1.y < d2.y)? d1.y: d2.y;               //that reference year
        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 no. of days elapsed past the reference year
    }
    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!

提交回复
热议问题