How does a C-language applet determine the difference between two dates?

Source: Internet
Author: User
Tags date1

Enter two dates and calculate the number of days for the difference. Two methods are used for implementation. The second method uses struct, and the code is clearer. The rest are the same.

1. Common writing

Copy codeThe Code is as follows:
# Include <stdio. h>
Int leapyear (int year)
{
If (year % 4 = 0 & amp; year % 100! = 0) | year % 400 = 0)
Return 1;
Else
Return 0;
}
Int days (int * day1, int * day2)
{
Int I = 0;
Int * tmp;
Int diff = 0;
Const int month [13] = {, 31, 31, 31 };
If (day1 [0] = day2 [0])
{
If (day1 [1] = day2 [1])
{
Diff = day1 [2]-day2 [2];
Diff = (diff <0 )? (-Diff): diff;
}
Else
{
If (day1 [1] <day2 [1]) // day1 = 1991-5-8 day2 = 1991-6-2
{
Tmp = day1; // day1 = 1991-6-2 day2 = 1991-5-8
Day1 = day2;
Day2 = tmp;
}
For (I = day2 [1] + 1; I <day1 [1]; I ++)
{
Diff + = month [I];
}
Diff + = month [day2 [1]-day2 [2] + day1 [2];
If (day2 [1] <= 2 & day1 [1]> 2)
If (leapyear (day2 [0])
Diff ++;
}
}
Else
{
If (day1 [0] <day2 [0])
{
Tmp = day1;
Day1 = day2;
Day2 = tmp;
}
For (I = day2 [0] + 1; I <day1 [0]; I ++)
{
If (leapyear (I ))
Diff ++ = 366;
Else
Diff ++ = 365;
}
For (I = day2 [1] + 1; I <= 12; I ++) // day1 = 1992-1-1 day2 = 1991-1-1
{
Diff + = month [I];
}
Diff + = (month [day2 [1]-day2 [2]);
If (day2 [1] <= 2)
If (leapyear (day2 [0])
Diff ++;
For (I = 1; I <day1 [1]; I ++)
{
Diff + = month [I];
}
Diff + = day1 [2];
If (day1 [1]> 2)
If (leapyear (day1 [0])
Diff ++;
}
Return diff;
}
Int main ()
{
Int day1 [3], day2 [3];
Int day = 0;
Printf ("input date :");
Scanf ("% d-% d", & day1 [0], & day1 [1], & day1 [2]);
Printf ("enter another date :");
Scanf ("% d-% d", & day2 [0], & day2 [1], & day2 [2]);
Day = days (day1, day2 );
Printf ("two dates have a total of % d days. N ", day );
Return 0;
}


2. Use struct to make the code more clean

Copy codeThe Code is as follows:
# Include <stdio. h>
Typedef struct date
{
Int year;
Int month;
Int day;
} DATE;
Int leapyear (int year)
{
If (year % 4 = 0 & amp; year % 100! = 0) | year % 400 = 0)
Return 1;
Else
Return 0;
}
Int compare (DATE * d1, DATE * d2) // if the first DATE is greater than the second one, exchange the DATE
{
DATE * tmp;
If (d1-> year = d2-> year) // The number of years is equal.
{
If (d1-> month> d2-> month) // The number of months is equal.
{
Tmp = d1;
D1 = d2;
D2 = d1;
}
Else if (d1-> month = d2-> month) // Date is equal
{
If (d1-> day> d2-> day)
{
Tmp = d1;
D1 = d2;
D2 = d1;
}
}
}
Else if (d1-> year> d2-> year)
{
Tmp = d1;
D1 = d2;
D2 = tmp;
}
Return 0;
}
Int diff (DATE * date1, DATE * date2)
{
Int I;
Int diff = 0;
Const int month [13] = {, 31, 31, 31 };
If (date1-> year = date2-> year)
{
If (date1-> month = date2-> month)
{
Diff = date2-> day-date1-> day;
}
Else
{
For (I = date1-> month + 1; I <date2-> month; I ++)
{
Diff + = month [I];
}
Diff + = month [date1-> month]-date1-> day + date2-> day;
If (leapyear (date1-> year ))
If (date1-> month <= 2 & date2-> month> 2)
Diff ++;
}
}
Else
{
For (I = date1-> year + 1; I <date2-> year; I ++)
{
If (leapyear (I ))
Diff ++ = 366;
Else
Diff ++ = 365;
}
For (I = date1-> month + 1; I <= 12; I ++) // how many days is date1 from the end of the year?
{
Diff + = month [I];
}
Diff + = month [date1-> month]-date1-> day;
If (date1-> month <= 2)
If (leapyear (date1-> year ))
Diff ++;
For (I = 1; I <date2-> month; I ++) // how many days is date2 from the beginning of the year?
{
Diff + = month [I];
}
Diff + = date2-> day;
If (date1-> month> 2)
If (leapyear (date2-> year ))
Diff ++;
}
Return diff;
}
Int main ()
{
Int days = 0;
DATE day1, day2;
DATE * date1, * date2;
Date1 = & day1;
Date2 = & day2;
Printf ("input date :");
Scanf ("% d-% d", & (date1-> year), & (date1-> month), & (date1-> day ));
Printf ("enter another date :");
Scanf ("% d-% d", & date2-> year, & date2-> month, & date2-> day );
Compare (date1, date2 );
Days = diff (date1, date2 );
Printf ("two dates have a total of % d days. N ", days );
Return 0;
}

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.