Skip to content

Differnece Start and end date check no of months

vidya kumari

Asked Apr 28, 2012, 10:00 PM EDT1 answer
Share
LinkedInX
I  have  three  text  StartTextbox  and  EndTextBox  ,Textbox3 .Please  careful  textbox3(No  of  month).  Now  I  want  difference  of  start  date  and  end  date  is  Checked  by  No  of  Months  .
 
 
Here  function  in  customvalidation 
protected void ValidateDuration(object sender, ServerValidateEventArgs e)
  {
  DateTime start = DateTime.Parse(StartTextBox.Text);
  DateTime end = DateTime.Parse(EndTextBox.Text);
 
 
  int months = (end.Month - start.Month) + 12 * (end.Year - start.Year);
 
   e.IsValid = months <=TextBox3;
  }

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Senthilkumar
    0
    Hi Vidya,

    Try this, it  will give you exactly number of months between two dates.

    C#
    private static int monthDifference(DateTime startDate, DateTime endDate)
    {
    int monthsApart = 12 * (startDate.Year - endDate.Year) + startDate.Month - endDate.Month;
    return Math.Abs(monthsApart);
    }
    0