Hi
I need to calculate difference between two days and result required
1) Difference in year month & day.
2) First day is included as working day. means from 1st Jan to 31 Jan is 1 Month not 30 Days.
So I implemented logic:
A : Get the difference b/w both date.
B: Add difference in base date.
B 1) base date: year 0001, month : as from date's month: day as from date's day
B 2) if from date is leap year than base date: year 0004, month : as from date's month: day as from date's day
C: check if from_date's month is greater than to date than subtract 1 year from from_date & add 12 months.
D: check if from_date's day is greater than to date than subtract 1 month from from_date & add days of current month of to_date.
snips of code is:
DateTime dd = this.fromDate.AddDays(-1);//Becuase we including first day in calculation
DateTime dd2 = this.toDate;
TimeSpan ts = dd2.Subtract(dd);
DateTime dd3 = new DateTime();
int yearsubtrutin = 1;
if ((this.fromDate.Year % 4) == 0)
{
dd3 = new DateTime(0004, this.fromDate.Month, this.fromDate.Day);
yearsubtrutin = 4;
}
else
{
dd3 = new DateTime(0001, this.fromDate.Month, this.fromDate.Day);
yearsubtrutin = 1;
}
dd = this.fromDate;
dd3 = dd3.Add(ts);
this.year = dd3.Year - yearsubtrutin;
this.month = dd3.Month - this.fromDate.Month;
if (this.month < 0)
{
this.year = this.year - 1;
this.month = this.month + 12;
}
this.day = dd3.Day - this.fromDate.Day;
if (this.day < 0)
{
this.day = this.day + Getdays(this.toDate.Month, yearsubtrutin);
this.month = this.month - 1;
}
//
}
public int Getdays(int monthNumber, int isleap)
{
if (false)
return 29;
else
{
switch (monthNumber)
{
case 1:
return 31;
break;
case 2:
if (isleap == 4)
return 29;
else
return 28;
break;
case 3:
return 31;
break;
case 4:
return 30;
break;
case 5:
return 31;
break;
case 6:
return 30;
break;
case 7:
return 31;
break;
case 8:
return 31;
break;
case 9:
return 30;
break;
case 10:
return 31;
break;
case 11:
return 30;
break;
case 12:
return 31;
break;
default:
break;
}
}
return 0;
}
Thank
Mahesh