C language implementation, calculate the number of days of difference between two days

Source: Internet
Author: User

Given the year, month, and day of the two days, calculate the number of different days, and the year is small to large.

# Include <stdio. h> // for a given year, month, and day, calculate the number of days that have passed in the year int total_day (int year, int month, int day) {int sum = 0; switch (month) {case 1: sum = day; break; case 2: sum = 31 + day; break; case 3: sum = 59 + day; break; case 4: sum = 90 + day; break; case 5: sum = 120 + day; break; case 6: sum = 151 + day; break; case 7: sum = 181 + day; break; case 8: sum = 212 + day; break; case 9: sum = 243 + day; break; case 10: sum = 273 + d Ay; break; case 11: sum = 304 + day; break; case 12: sum = 334 + day; break; default: printf ("input month error \ n"); break;} if (month> 2) {if (year % 4 = 0) & (year % 100! = 0) | (year % 400) = 0) {sum = sum + 1 ;}} return sum ;} // number of days between the year and the year int total_year_day (int year1, int year2) {int sum_year_day = 0; int I = 0; sum_year_day = (year2-year1) * 365; for (I = year1; I <year2; I ++) {if (I % 4 = 0) & (I % 100! = 0) | (I % 400) = 0) {sum_year_day = sum_year_day + 1 ;}return sum_year_day;} int main () {int year1 = 2013, month1 = 1, day1 = 1; int year2 = 2013, month1 = 1, day2 = 1; int sum = 0; printf ("~~~~~~ Calculates the number of days for which the two days are different ~~~~~~ \ N "); printf (" Enter the start year, month, and day (Format: XXXX: XX) "); scanf (" % d: % d ", & year1, & month1, & day1); printf ("Enter the end year, month, and day (Format: XXXX: XX)"); scanf ("% d: % d ", & year2, & mon2, & day2); sum = total_year_day (year1, year2)-total_day (year1, month1, day1) + total_day (year2, mon2, day2); printf ("the number of days between them is % d \ n", sum); 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.