How to get end date

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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);

Method 2

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.

Method 3

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x