C Language Applet How to judge the difference between two dates _c language

Source: Internet
Author: User
Tags date1 diff
1. The common wording
Copy Code code as follows:

#include <stdio.h>
int leapyear (int year)
{
if ((year%4==0 && 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]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if (day1[0] = = Day2[0])
{
if (day1[1] = = day2[1])
{
diff = day1[2]-day2[2];
diff = (diff < 0)? (-diff):d iff;
}
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 ("Enter Date:");
scanf ("%d-%d-%d", &day1[0], &day1[1], &day1[2));
printf ("Enter another date:");
scanf ("%d-%d-%d", &day2[0], &day2[1], &day2[2));
Day = Days (Day1, day2);
printf ("There are%d days between two dates.") \ n ", day);
return 0;
}

2. Use the structure, the code is more tidy some
Copy Code code as follows:

#include <stdio.h>
typedef struct DATE
{
int year;
int month;
int day;
}date;
int leapyear (int year)
{
if ((year%4==0 && year%100!=0) | | | year%400==0)
return 1;
Else
return 0;
}
int compare (date *d1, date *d2)//If the first date is larger than the second date, swap date
{
DATE *tmp;
if (d1->year = = d2->year)//year equal
{
if (D1->month > D2->month)//number of months equal
{
TMP = D1;
D1 = D2;
D2 = D1;
}
else if (D1->month = = d2->month)//Date 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]={0,31,28,31,30,31,30,31,31,30,31,30,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++)//date1 How many days 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++)//date2 distance from the beginning how many days
{
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 ("Enter Date:");
scanf ("%d-%d-%d",& (Date1->year), & (Date1->month), & (Date1->day));
printf ("Enter another date:");
scanf ("%d-%d-%d", &date2->year, &date2->month, &date2->day);
Compare (Date1, date2);
Days = diff (Date1, date2);
printf ("There are%d days between two dates.") \ 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.