Reputation: 95
As the title says I want to calculate the difference between two given dates without using any library function such as difftime. If the answer be an algorithm it will be better...
Upvotes: 1
Views: 6506
Reputation: 11
int getDifferentDay(int d1, int m1, int y1, int d2, int m2,int y2) { int x1,x2;
m1 = (m1 + 9) % 12;
y1 = y1 - m1 / 10;
x1= 365*y1 + y1/4 - y1/100 + y1/400 + (m1*306 + 5)/10 + ( d1 - 1 );
m2 = (m2 + 9) % 12;
y2 = y2 - m2 / 10;
x2= 365*y2 + y2/4 - y2/100 + y2/400 + (m2*306 + 5)/10 + ( d2 - 1 );
return (x2 - x1);
}
Upvotes: 1
Reputation: 83018
I would like to tell you in what way you should proceed
First Write few functions ...
A. int valiDate(char *date_string);
This function should take the date as the input entered in the form of a string. It should check whether its a proper Date or Not. I hope You can do this... Reply Back telling me the details of What are the things you checked to take care if its a valid date.It should return 1 if its a proper date or else it should return 0.
B. int year(char *date_string);
This function should return the Year of the Given date. remember you should validatee the date_string befotre returning the year. If the date is not proper it should return a Negative value.
C. int month(char *date_string);
This function should return the MOnth of the Given date. remember you should validatee the date_string befotre returning the month. If the date is not proper it should return a Negative value.
D. int date(char *date_string);
This function should return the date value of the Given date. remember you should validatee the date_string befotre returning the date. If the date is not proper it should return a Negative value.
E. int number_of_days_in_the_year(int year);
This function should take an integer year in the form of input and then return the number of days in that year.
For Eg -
if input is 2004, output should be 366
if input is 2005, output should be 365
F. int number_of_days_till_now(char *date_string);
This function should return the number of days over till the given date in the given year. For eg Note in all examples i consider the date input string in the DD_MM_YY format.
Input Output
23-02-2004 (Feb 23 2004) 54
01-03-2004 (Mar 1 2004) 61
15-04-2003 (Apr 15 2003) 105
01-03-2003 (Mar 1 2003) 60
I hope You understood this.
Now Lets look at the algorithm
if 'year_diff' > 1 then a. Calculate the number of days in the years between dmy1 and dmy2. For eg if dmy2= 15-03-2005 and dmy1=16-02-2001 then the differenc between the years 'year_diff'=(2005-2001)=4 . Therefore Calculate the total number of days in years 2002, 2003,2004 using function 'number_of_days_in_the_year' (function 5) Add them, lets say its stored in "SUM".
Calcualte the value SUM1 = number_of_days_till_now(dmy2); i.e number of days over in the dmy2. In our example it is SUM1 = number_of_days_till_now(15-03-2005) = 74;
Calculate the value Y2 = number_of_days_in_the_year (dmy1) . I.e Total Number of days in year 1. In our Example it is Y2 = number_of_days_in_the_year (16-02-2001) = 365;
Calculate the value SUM2= Y2 - number_of_days_till_now(dmy1); ie. 365 - 47 = 318 ;
Now add SUM + SUM1 + SUM2 , You will get the difference between the dates dmy2 and dmy1;
And here is an sample code :
#include<stdio.h>
#include<math.h>
void main()
{
int day1,mon1,year1,day2,mon2,year2;
int ref,dd1,dd2,i;
clrscr();
printf("Enter first day, month, year");
scanf("%d%d%d",&day1,&mon1,&year1);
scanf("%d%d%d",&day2,&mon2,&year2);
ref = year1;
if(year2<year1)
ref = year2;
dd1=0;
dd1=func1(mon1);
for(i=ref;i<year1;i++)
{
if(i%4==0)
dd1+=1;
}
dd1=dd1+day1+(year1-ref)*365;
printf("No. of days of first date fronm the Jan 1 %d= %d",year1,dd1);
/* Count for additional days due to leap years*/
dd2=0;
for(i=ref;i<year2;i++)
{
if(i%4==0)
dd2+=1;
}
dd2=func1(mon2)+dd2+day2+((year2-ref)*365);
printf("No. of days from the reference year's first Jan = %d",dd2);
printf("Therefore, diff between the two dates is %d",abs(dd2-dd1));
getch();
}
Happy coding.
Upvotes: 0
Reputation: 32898
Here is some code I just wrote to calculate the difference in years, months, and days. It is in the public domain.
public sealed class DateDifference {
int years;
public int Years {
get { return years; }
}
int months;
public int Months {
get { return months; }
}
int days;
public int Days {
get { return days; }
}
public override string ToString()
{
return string.Format("[DateDifference Years={0}, Months={1}, Days={2}]", years, months, days);
}
public DateDifference(DateTime earlier, DateTime later){
if(later<earlier)
throw new ArgumentException("later is earlier than 'earlier'.");
bool isleapday=(earlier.Month==2 && earlier.Day==29);
DateTime tmp=isleapday ? new DateTime(earlier.Year,2,28) : earlier;
while(true){
try {
tmp=tmp.AddYears(1);
if(isleapday && DateTime.IsLeapYear(tmp.Year))
tmp=new DateTime(tmp.Year,2,29);
} catch(ArgumentOutOfRangeException){
break;
}
if(tmp<=later){
years++;
earlier=tmp;
} else {
break;
}
}
// Add months
tmp=earlier;
while(true){
try {
tmp=tmp.AddMonths(1);
if(isleapday && tmp.Day!=29 && tmp.Month!=2)
tmp=new DateTime(tmp.Year,tmp.Month,29);
} catch(ArgumentOutOfRangeException){
break;
}
if(tmp<=later){
months++;
earlier=tmp;
} else {
break;
}
}
tmp=earlier;
while(true){
try {
tmp=tmp.AddDays(1);
} catch(ArgumentOutOfRangeException){
break;
}
if(tmp<=later){
days++;
earlier=tmp;
} else {
break;
}
}
}
}
Upvotes: 0