Enter two dates (month and month date) to output the number of days between these two dates

Source: Internet
Author: User

Assume that the input date is valid, and that the second date is later than the first date.

My idea is to first judge is not the same year, if it is, then the second date in the year of the number of days minus the first date in the year can be;

If not, just divide the number of days in the middle interval into three parts, the first part is the number of days from the first date to the end of the year, and the second is the number of days from the first day of the second year of the first date to the last day of the previous year of the second date, and the third is the number of days from the first Three parts are quite equal to the difference in number of days.

In particular, it is important to remember that leap years must be judged.

1 #ifndef __fun_h__2 #define__fun_h__3#include <stdio.h>4#include <stdlib.h>5 6 unsigned days_in_year (unsigned y, unsigned m, unsigned d);7 intis_leap_year (unsigned y);8 #endif9 Ten //given two dates, how many days are there between these two dates?  One#include"func.h" A  - intMainintargcChar*argv[]) - { theUnsigned y1, M1, D1, y2, M2, D2, I, n =0; -     intRes; -  -      while(Fflush (stdin), (res = scanf ("%d%d%d%d%d%d", &y1, &m1, &d1, &y2, &m2, &d2))! =EOF) +     { -n =0; +         if(Y1! =y2) A         { at             if(Is_leap_year (y1)) -n + =366-days_in_year (Y1, M1, D1); -             Else  -n + =365-days_in_year (Y1, M1, D1); -              for(i = y1 +1; i < Y2; i++) -             { in                 if(Is_leap_year (i)) -n + =366; to                 Else +n + =365; -             } then + =days_in_year (y2, M2, D2); *         } $         ElsePanax Notoginsengn = days_in_year (y2, M2, D2)-days_in_year (Y1, M1, D1); -printf"The above two dates are%d days apart \", n); the     } +System"Pause"); A     return 0; the } +  -  $ unsigned days_in_year (unsigned y, unsigned m, unsigned d) $ { -unsigned n =0, I; -unsigned months[ -] = {0, to, -, to, -, to, -, to, to, -, to, -, to}; the  -     if(Is_leap_year (y))Wuyimonths[2] = in; the     Else  -months[2] = -; Wu      for(i =1; I < m; i++) -n + =Months[i]; Aboutn + =D; $     returnN; - } -  -  A intis_leap_year (unsigned y) + { the     if((Y%4==0&& y% -!=0) || Y% -==0) -         return 1; $     Else  the         return 0; the}

PS: The Zhaogo method is relatively simple and is hereby recorded.

Assuming that the input date is 1988 5 1 2015 3 9

The number of days from January 1, 1988 to January 1, 2015 can be calculated directly. Then subtract May 1, 1988 days in 1988, plus the number of days of March 9, 2015 in 2015.

The two date entered is not judged by the same year.

Enter two dates (month and month date) to output the number of days between these two dates

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.