计算由于行程时间百分比变化引起的时间差异

时间:2015-02-09 05:30:10

标签: c# time

我对编程很新,刚刚遇到这个网站,我希望我可以得到一点点帮助。

我的目标是确定新的交货时间,并在原始交货时间内加上25%的延迟。这将在Windows窗体应用程序中完成。用户将在各自的文本框中输入原始传送开始和结束时间,单击按钮时,新的传送时间将显示在其各自的文本框中。这就是我到目前为止所做的。

 private void button1_Click(object sender, EventArgs e)
    {
        const double PERCENT = 0.25;


        DateTime t1 = Convert.ToDateTime(txtB_StartT.Text);
        DateTime t2 = Convert.ToDateTime(txtB_EndT.Text);
        TimeSpan ts = t1.Subtract(t2);
        double tsMin = (double)Convert.ToDouble(ts);
        double tsMinTot = ts.TotalMinutes;
        short tsMinPercent = (short)(((double)tsMinTot) * PERCENT);
        double tsAndPercentTot = tsMinPercent + tsMinTot;
        DateTime newTimeMinTot = Convert.ToDateTime(tsAndPercentTot);

        int hours = newTimeMinTot.Hour;
        int minutes = newTimeMinTot.Minute;

        string newTimeStrg = string.Format("{0:d1}:{1:d2}", hours, minutes);


        txtB_NewDelivT.Text = newTimeStrg;

所以我试图绕过这个问题,但仍然犯错误

1 个答案:

答案 0 :(得分:1)

我认为这可以满足您的需求:

    //Initial Delivery 5 Days
    DateTime start = DateTime.Now; //User Enters todayy as start
    DateTime end = start.AddDays(5); //User enters 5 days from now as end.

    //Get the ticks between now and 5 days time
    long ticksDiff = end.Ticks - start.Ticks;

    //Calculate 25% of difference
    long percentOfTicks = (long)(((double)ticksDiff) * 0.25);

    //This is your new date (orifiginal delivery date + 25%)
    DateTime newDelayedStart = end.AddTicks(percentOfTicks);