Question

How to get End Date after selection of Start date from drop down list.

I am selecting startdate from dropdowns and I am showing last date in label.

For example- If I am selecting "January" from first dropdown. Date "1" from second dropdown. Then Label1.text become last date i.e. 31 december.

How can I do this ?

enter image description here

Was it helpful?

Solution

Please try below code

     int month = DateTime.ParseExact(Convert.ToString(ddlMonth.SelectedValue), "MMMM", CultureInfo.CurrentCulture).Month;

    int day = Convert.ToInt32(ddlDay.SelectedValue);
    int year=DateTime.Now.Year;
    DateTime date = new DateTime(year,month, day);
    //Use AddDays for add and substract  days
    date.AddDays(-1);
   string str=String.Format("{0:m}", date);

OTHER TIPS

There are many ways of doing it . You can do it in javascript as well as in asp.net. have a page method of make a $.ajax call with the data selected

               $.ajax({
                   url : '',
                   data : 'month=MONTH&day=DAY',
                   success : function(result){
                     $("#labelid").text(result);
                  }
               })

C# part

                  int maxDay = DateTime.DaysInMonth(DateTime.Now.year,month); 
                  //validate the selected day is equal or less than the maxDay 
                  DateTime StartDate = new DateTime(DateTime.Now.Year, Convert.ToInt16(dropdownMonth.SelectedIndex) + 1, Convert.ToInt16(dropdownDays.SelectedIndex) + 1);
                  DateTime PreDayDate = StartDate.AddDays(-1); lblEndDateValue.Text = PreDayDate.ToString();

In case you do not want to do AJAX you have to do the postback and handle it then onward.

using DateTime.AddDays Method you can do this

DateTime StartDate = new DateTime(DateTime.Now.Year, Convert.ToInt16(UrMonthNameDropDown.SelectedIndex+1), Convert.ToInt16(UrDateDropdown.SelectedIndex));



DateTime PreDayDate = StartDate.AddDays(-1); 

substract 1 day from your start date.

source:http://msdn.microsoft.com/en-IN/library/system.datetime.adddays.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top