C# Code
public string CalculateTenure(DateTime Hday, DateTime Cday)
{
int Years =
0; int Months
= 0;
if
((Cday.Year - Hday.Year) > 0 || (((Cday.Year - Hday.Year) == 0) &&
((Hday.Month < Cday.Month) || ((Hday.Month ==
Cday.Month) && (Hday.Day <= Cday.Day)))))
{
int
DaysInHdayMonth = DateTime.DaysInMonth(Hday.Year, Hday.Month);
int DaysRemain = Cday.Day + (DaysInHdayMonth - Hday.Day);
if (Cday.Month > Hday.Month)
{
Years
= Cday.Year - Hday.Year;
Months = Cday.Month - (Hday.Month + 1) + Math.Abs(DaysRemain /
DaysInHdayMonth);
}
else if (Cday.Month == Hday.Month)
{
if (Cday.Day >= Hday.Day)
{
Years
= Cday.Year - Hday.Year;
Months
= 0;
}
else
{
Years
= (Cday.Year - 1) - Hday.Year;
Months = 11;
}
}
else
{
Years
= (Cday.Year - 1) - Hday.Year;
Months = Cday.Month + (11 - Hday.Month) + Math.Abs(DaysRemain /
DaysInHdayMonth);
}
}
return Years + " Year(s)
" + Months + " Month(s)";
}
Javascript Code
function CalculateTenure(Hday, Cday) {
var Years = ''; var Months = '';
var HdayDate = new Date(Hday);
var CdayDate = new Date(Cday);
var HdayDateYear = HdayDate.getFullYear();
var HdayDateMonth = HdayDate.getMonth() + 1;
var HdayDateDay = HdayDate.getDate();
var CdayDateYear = CdayDate.getFullYear()
var CdayDateMonth = CdayDate.getMonth() + 1;
var CdayDateDay = CdayDate.getDate();
if ((CdayDateYear - HdayDateYear) > 0 || (((CdayDateYear
- HdayDateYear) == 0) && ((HdayDateMonth < CdayDateMonth) ||
((HdayDateMonth == CdayDateMonth) && (HdayDateDay <=
CdayDateDay)))))
{
var DaysInHdayMonth = new Date(HdayDateYear,
HdayDateMonth, 0).getDate();
var DaysRemain = CdayDateDay + (DaysInHdayMonth - HdayDateDay);
if (CdayDateMonth > HdayDateMonth)
{
Years
= CdayDateYear - HdayDateYear;
Months = CdayDateMonth - (HdayDateMonth + 1)
+ Math.floor(DaysRemain / DaysInHdayMonth);
}
else if (CdayDateMonth == HdayDateMonth)
{
if (CdayDateDay >=
HdayDateDay)
{
Years = CdayDateYear - HdayDateYear;
Months = 0;
}
else
{
Years
= (CdayDateYear - 1) - HdayDateYear;
Months = 11;
}
}
else
{
Years
= (CdayDateYear - 1) - HdayDateYear;
Months
= CdayDateMonth + (11 - HdayDateMonth) + Math.floor(DaysRemain / DaysInHdayMonth);
}
}
return Years + "
Year(s) " + Months + " Month(s)";
}
No comments:
Post a Comment