-1

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

Azhar Shahid
  • 151
  • 1
  • 4
  • 23
  • 1
    What do want to do ? Can you share the code ? Could not got your business requirement ? What is the problem you are facing – Devesh May 14 '14 at 07:02
  • Indeed, your requirements are not really clear... You could assign a postback event if you want to do it in code behind. You can also act on a dropdown change event using javascript. – emp May 14 '14 at 07:05
  • I want that whenever I select start date from dropdown, end date on label show the date which comes before selected date. – Azhar Shahid May 14 '14 at 07:06
  • possible duplicate of [how to change second drop down item on selection from first drop down list](http://stackoverflow.com/questions/23646062/how-to-change-second-drop-down-item-on-selection-from-first-drop-down-list) – Krunal Patil May 14 '14 at 07:06

3 Answers3

2

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);
Deepak Joshi
  • 1,036
  • 7
  • 17
1

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.

Devesh
  • 4,500
  • 1
  • 17
  • 28
  • @Devesh- how to do it using asp.net. – Azhar Shahid May 14 '14 at 07:13
  • I updated my answer. You are just missing the year. You can use the DateTime class to create a date out of the data you have. – Devesh May 14 '14 at 07:17
  • On doing this.... DateTime StartDate = new DateTime(DateTime.Now.Year, Convert.ToInt16(dropdownMonth.SelectedIndex), Convert.ToInt16(dropdownDays.SelectedIndex)); DateTime PreDayDate = StartDate.AddDays(-1); lblEndDateValue.Text = PreDayDate.ToString(); Getting error- Year, Month, and Day parameters describe an un-representable DateTime. – Azhar Shahid May 14 '14 at 07:24
  • check the value coming in the MonthSelectedIndex and remember in some case some of the month is only having 30 days. So you need to validate that also.I have updated my code to check this. I think month should start from 1 where selectedIndex start from 0 be careful – Devesh May 14 '14 at 07:27
  • On selection of January , Febuary and so on.. Getting value 0 to 11 respectively – Azhar Shahid May 14 '14 at 07:32
  • Please add + 1 to the month index as well as for day . See my comment above – Devesh May 14 '14 at 07:33
  • It's working but Dont want to show year and time and month like-- January, febuary. – Azhar Shahid May 14 '14 at 07:36
  • Checkout the DateTime class , you have option to get everything from the date and format it. Check it out in the msdn . That is too easy to solve. Thanks – Devesh May 14 '14 at 07:38
0

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

Dgan
  • 10,077
  • 1
  • 29
  • 51