<pre>private void datediff(TextBox d1, Label lbl, TextBox d2 = null)
{
int days;
int months;
int years;
string date2;
if(d2 == null)
{
date2 = "31/08/2019";
}
else
{
date2 = d2.Text;
}
try
{
int bd = Convert.ToInt32(d1.Text.Split('/')[0]);
int bm = Convert.ToInt32(d1.Text.Split('/')[1]);
int by = Convert.ToInt32(d1.Text.Split('/')[2]);
int cd = Convert.ToInt32(date2.Split('/')[0]);
int cm = Convert.ToInt32(date2.Split('/')[1]);
int cy = Convert.ToInt32(date2.Split('/')[2]);
int[] mA = { 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
if ((cy % 4 == 0) && (cy % 100 != 0))
{
mA[1] = 29;
}
if (cd < bd)
{
if (cm == 1)
{
cm = cm + 12;
cy = cy - 1;
}
cm = cm - 1;
cd = cd + mA[cm - 1];
}
if (cm < bm)
{
cm = cm + 12;
cy = cy - 1;
}
days = cd - bd;
months = cm - bm;
years = cy - by;
if (years < 0)
{
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('From Date cannot be after the To Date')", true);
d1.Text = null;
if (d2 != null)
{
d2.Text = null;
}
lbl.Text = null;
}
else
{
lbl.Text = years + " Year " + months + " Month " + days + " Day";
lbl.Attributes.CssStyle.Add("color", "#3e4095");
}
}
catch (Exception ex)
{
lbl.Text = "Please Fill value in correct format (dd/MM/yyyy)";
lbl.Attributes.CssStyle.Add("color", "red");
d1.Text = null;
if (d2 != null)
{
d2.Text = null;
}
}
}