calculate the days between two dates

前端 未结 2 1005
傲寒
傲寒 2021-01-29 13:18

I did a program that calculates the days different, between two dates, but I am not sure how can I add a statement that makes sure the program wont include the end date.

2条回答
  •  深忆病人
    2021-01-29 14:00

    my code is calculating month and days but only printing days, its 86 days or 2 months and 25 days! how do i edit that so it shows 86?

    This is a typical case of "Don't (try to) reinvent the wheel." Use the Standard C mktime function to convert broken-down times into time values which can be subtracted from each other.

    #include 
    …
      time_t t1, t2;
      t1 = mktime(&(struct tm){ .tm_mday=dd1, .tm_mon=mm1-1, .tm_year=yyyy1-1900 });
      t2 = mktime(&(struct tm){ .tm_mday=dd2, .tm_mon=mm2-1, .tm_year=yyyy2-1900 });
      day_diff = (t2 - t1) / 24 / 60 / 60 + include;    // make days from seconds
    

提交回复
热议问题