C + + calculates the number of days between two dates

Source: Internet
Author: User

The idea of calculating the number of days between two dates:

First, determine whether the year entered is a leap years. Is the year the same? Is the month the same? Is the day the same?

There are three possible scenarios for the sun and moon year:

    1. Same year in the same month. The number of days to subtract is out.
    2. Same year different months. Calculates the number of days in a small month to the beginning of the day, and calculates the number of days from the month to the beginning. Two more dates to reduce
    3. Different years. Calculate the middle of a few years apart, calculate the number of days to the end of a smaller date, and then calculate the number of days from the beginning of the larger date, and add three numbers to it.

The code is as follows:

1#include <iostream>2#include <CString>3#include <cmath>4 using namespacestd;5 6 BOOLIsleapyear (intYear//determine if a leap year7 {8     if(Year%4==0&& Year% -!=0|| Year% -==0)9         return 1;Ten     Else One         return 0; A } -  - intDays (intYintMintD//Calculate y years m months D days to Y year January 1 days the { -     intDays[] = {0, to, -, to, -, to, -, to, to, -, to, -, to }; -     inti; -     intsum =0;/*Count Days*/ +     if(Isleapyear (y))/*if it is a leap year, February has 29 days*/    -days[2] = in; +      for(i =0; i<m; i++) Asum = sum +Days[i]; atsum = sum + D-1; -     returnsum; - } -  - intDays (intY1,intM1,intD1,intY2,intM2,intD2)//calculates the number of days between two dates that are not the same as the month and day - { in     intT1; -     intT2; to     intYear = y2-Y1; +     intsum=0;//Days of difference -     if(Year = =1)//if the year of two dates differs by 1 the     { *T1 = Days (y1, A, to)-Days (Y1, M1, D1);//Smaller date calculates Y1 year M1 month D1 day to end of days $T2 = days (y2, M2, D2);//A Larger date calculates the number of days y2 m2 months D2 days to the beginning of the yearPanax Notoginsengsum = t1 + t2 +Year ; -     } the     Else{ +          for(inti = y1+1; i < Y2; i++) A         { the             if(Isleapyear (i)) +             { -Sum + =366; $             } $             Else -Sum + =365; -         } theT1 = Days (y1, A, to) -Days (Y1, M1, D1); -t2 =Days (y2, M2, D2);Wuyisum = sum + T1 + t2 +1; the          -     } Wu     returnsum; - } About  $ intMain () { -     intyear1;//smaller year -     intyear2; -     intMonth1;//the smaller month A     intmonth2; +     intDay1; the     intDay2; -     intday=0; $CIN >> year1 >> month1 >>Day1; theCIN >> year2 >> month2 >>Day2; the  the     if(Year1 > Year2 | | (year1 = = year2) && (month1 > month2) | | (year1 = = year2) && (month1 = = month2)) && (Day1 >day2)) the     { -cout <<"Input Error"<<Endl; in     } the     Else{ the  About         if(Year1 = = Year2&&month1 = = month2)//1. If the year and month are the same, calculate the difference of the number of days directly the         { theDay = ABS (Day2-day1); thecout << Day <<Endl; +         } -         Else if(year1 = = Year2 && month1! = month2)//2. If the year is the same and the month is different, calculate the number of days from the smaller date to the larger date the         {BayiDay = ABS (Days (YEAR1, Month1, year1)-Days (Year2, Month2, Day2)); thecout << Day <<Endl; the         } -         Else -         { theDay =Days (year1, Month1, Day1, Year2, Month2, day2); thecout << Day <<Endl; the         } the     } -     return 0; the}

For the understanding of some code:

intDays (intYintMintD//Calculate y years m months D days to Y year January 1 days{    intDays[] = {0, to, -, to, -, to, -, to, to, -, to, -, to }; inti; intsum =0;/*Count Days*/    if(Isleapyear (y))/*if it is a leap year, February has 29 days*/days[2] = in;  for(i =0; i<m; i++) Sum= Sum +Days[i]; Sum= sum + D-1; returnsum;}

The understanding of this code can be expressed by:

We need to ask for a date of two red arrows between the green section.

1  for(inti = y1+1; i < Y2; i++)2         {3             if(Isleapyear (i))4             {5Sum + =366;6             }7             Else8Sum + =365;9         }TenT1 = Days (y1, A, to) -Days (Y1, M1, D1); Onet2 =Days (y2, M2, D2); Asum = sum + T1 + t2 +1;

The understanding of this code can be expressed by:

2015 with 2011 years of difference in fact only three years, just start writing when the number of years directly written is 2015 minus 2011, resulting in more than a year.

C + + calculates the number of days between 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.