How many days is the difference between two days in C ++?

Source: Internet
Author: User

The calculation principle is to first find the difference of the number of days between each date and January 1, January 1, and then perform the difference.

CopyCode The Code is as follows: # include <stdio. h>
Struct mydate
{
Int year;
Int month;
Int Day;
};

Int getabsdays (mydate X)
{
Int I;
Int month_day [] = {31,28, 31,30, 31,30, 31,31, 30,31, 30,31 };
Int year = x. Year-1; // The distance from January 1, January 1.
Int days = year * 365 + year/4-year/100 + year/400; // calculate the number of previous leap years and add the number of days
If (X. Year % 4 = 0 & X. Year % 100! = 0 | X. Year % 400 = 0) month_day [1] ++; // the current year is a leap year, and 1 is added on January 1, February.
For (I = 0; I <X. Month-1; I ++)
Days + = month_day [I];
Days + = x. Day-1; // today is not counted as the number of days
Return days;
}

Int getdiffdays (mydate A, mydate B)
{
Return getabsdays (B)-getabsdays ();
}

Int main (INT argc, char * argv [])
{
Mydate A = {1842,5, 18 };
Mydate B = {2000,3, 13 };
Int n = getdiffdays (A, B );
Printf ("% d \ n", N );
}

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.