How to get end date

2019-01-29 13:33发布

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

3条回答
爷、活的狠高调
2楼-- · 2019-01-29 14:05

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.affffdays.aspx

查看更多
Explosion°爆炸
3楼-- · 2019-01-29 14:12

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);
查看更多
我命由我不由天
4楼-- · 2019-01-29 14:21

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.

查看更多
登录 后发表回答