c#,2个日期选择器,1个文本框

时间:2014-11-26 18:01:00

标签: c#

如果我在表单上有2个日期选择器供客户选择租赁开始日期和租赁返回日期,那么代码会是什么样的代码,计算两个日期之间的差异并将其放入文本框中。

我希望客户能够选择租赁开始和结束日期。然后计算然后在文本框中输入#天数,然后使用在另一个计算中在文本框中输入的数字。

我有dtpStartDate,dtpReturnDate& txtDays 这将是我的btnCalc_Click事件处理程序下的代码的一部分。 我是否需要声明StartDate& ReturnDate首先作为变量? 我有一个可变的日子,我已经在另一个计算中使用。

      /* This method calculates vehicle cost, add ons, discount and rental total cost
     */
    private void btnCalculate_Click(object sender, EventArgs e) {
        decimal addItemsCost = 0.0m;                //Cost of the selected additional items
        decimal rentalTotal;                        //Total cost of the rental
        int days;                                   //Number of rental days
        decimal discountTotal = 0.0m;               //Total discount if promotion criteria met

        //Transfer inputs to variables
        days = int.Parse(vTools.unformat(txtDays.Text));

        //Vehicle cost determined by rdoVehicle Check Changed

        //Calculate additional item cost
        if (chkDvd.Checked)         addItemsCost += DVDCOST;
        if (chkAir.Checked)         addItemsCost += AIRCOST;
        if (chkCarSeat.Checked)     addItemsCost += CARSEATCOST;
        if (chkInsurance.Checked)   addItemsCost += INSURANCECOST;


        //Calculate the total cost of add on's and total rental cost
        if (days >= PROMODAYS && rdoLuxury.Checked) {
          rentalTotal = ((vehicleCost * days) + addItemsCost - discountTotal );
          discountTotal = rentalTotal * PDISCOUNT;

        } else {
            rentalTotal = (vehicleCost * days) + addItemsCost;
        }//end if else
          rentalTotal = (vehicleCost * days) + addItemsCost - discountTotal; 

1 个答案:

答案 0 :(得分:0)

将文本框中的两个日期解析为DateTime个对象EndDateStartDate,然后计算间隔:

var startDate = DateTime.Parse(myStartDateTextBox.Text);

var endDate  = DateTime.Parse(myEndDateTextBox.Text);

myIntervalTextBox.Text = (endDate - startDate).TotalDays.ToString();