Hi...
I need to count the number of days between 2 dates.
From a previous example in the notes I managed to come up with the following code which gives you the number of days but first converting the date to a julian date. Is there any other way of doing this? Plus...how do I go about if I want it to specify the exact number of years, months and days? Any ideas? Just ideas?Thanx.
[code tags added by ygfperson]Code:void read_date(int *dd, int *mm, int *yyyy) { int i; while(1) { printf("Enter date (DD MM YYYY): "); i = scanf("%d %d %d", dd, mm, yyyy); if(i == 3) { if((*mm < 1) || (*mm > 12)) { printf("MM must be between 1 and 12\n"); continue; } if((*dd < 1) || (*dd > days_month[*mm-1])) { printf("DD must be between 1 and %d for the month of %s\n", days_month[*mm-1], months[*mm-1]); continue; } if((*yyyy < 1) || (*yyyy > 9999)) { printf("YYYY must be between 0 and 9999\n"); continue; } } else { continue; } break; } } int leap_year(int yyyy) { return((yyyy % 400 == 0) || (yyyy % 4 == 0 && yyyy % 100 != 0)); } int convert_to_jdate(int dd, int mm, int yyyy) { int total_days = 0; if (yyyy < 1) return -1; total_days = (yyyy-1) * 365; total_days += (yyyy-1) / 4; total_days -= (yyyy-1) / 100; total_days += (yyyy-1) / 400; total_days += cumulative_days[mm-1]; total_days += dd; if(leap_year(yyyy)) if (mm > 2) total_days++; return total_days; } /*********************************************************************************************************/ void date() { int dd1, mm1, yyyy1; int dd2, mm2, yyyy2; int t1, t2, t_total; read_date(&dd1, &mm1, &yyyy1); read_date(&dd2, &mm2, &yyyy2); t1 = convert_to_jdate(dd1, mm1, yyyy1); t2 = convert_to_jdate(dd2, mm2, yyyy2); t_total = t1 - t2; if(t_total < 0) { t_total *= -1; } printf("There are %d day(s) between the two dates\n", t_total); return; }



LinkBack URL
About LinkBacks


