小数总小时数之和

时间:2014-02-28 20:18:55

标签: c# asp.net

我有一个网站,可以计算24小时制输入班次的小时数。现在,用户可以以hh:mm的格式键入时间和超时时间,并在单击“计算”按钮时以十进制形式计算该特定日期的班次。我需要的是按下相同按钮时计算和显示的十进制格式的总小时数(在整个2周时间内)。

以下是网站的外观,链接下方是我的“计算”按钮的代码。

enter image description here

protected void CaluclateButton_Click(object sender, EventArgs e)
{
    //Week 1 Calculations
    //Monday
    TimeSpan Mon1In, Mon1Out;
    if(!TimeSpan.TryParse(TextBoxInMon1.Text,out Mon1In)) Mon1In = default(TimeSpan);
    if(!TimeSpan.TryParse(TextBoxOutMon1.Text,out Mon1Out)) Mon1Out = default(TimeSpan);
    MonLabel1.Text = (Mon1Out - Mon1In).TotalHours.ToString("f2");
    //Tuesday
    TimeSpan Tues1In, Tues1Out;
    if (!TimeSpan.TryParse(TextBoxInTues1.Text, out Tues1In)) Tues1In = default(TimeSpan);
    if (!TimeSpan.TryParse(TextBoxOutTues1.Text, out Tues1Out)) Tues1Out = default(TimeSpan);
    TuesLabel1.Text = (Tues1Out - Tues1In).TotalHours.ToString("f2");
    //Wednesday
    TimeSpan Wed1In, Wed1Out;
    if (!TimeSpan.TryParse(TextBoxInWed1.Text, out Wed1In)) Wed1In = default(TimeSpan);
    if (!TimeSpan.TryParse(TextBoxOutWed1.Text, out Wed1Out)) Wed1Out = default(TimeSpan);
    WedLabel1.Text = (Wed1Out - Wed1In).TotalHours.ToString("f2");
    //Thursday
    TimeSpan Thurs1In, Thurs1Out;
    if (!TimeSpan.TryParse(TextBoxInThurs1.Text, out Thurs1In)) Thurs1In = default(TimeSpan);
    if (!TimeSpan.TryParse(TextBoxOutThurs1.Text, out Thurs1Out)) Thurs1Out = default(TimeSpan);
    ThursLabel1.Text = (Thurs1Out - Thurs1In).TotalHours.ToString("f2");
    //Friday
    TimeSpan Fri1In, Fri1Out;
    if (!TimeSpan.TryParse(TextBoxInFri1.Text, out Fri1In)) Fri1In = default(TimeSpan);
    if (!TimeSpan.TryParse(TextBoxOutFri1.Text, out Fri1Out)) Fri1Out = default(TimeSpan);
    FriLabel1.Text = (Fri1Out - Fri1In).TotalHours.ToString("f2");
    //Saturday
    TimeSpan Sat1In, Sat1Out;
    if (!TimeSpan.TryParse(TextBoxInSat1.Text, out Sat1In)) Sat1In = default(TimeSpan);
    if (!TimeSpan.TryParse(TextBoxOutSat1.Text, out Sat1Out)) Sat1Out = default(TimeSpan);
    SatLabel1.Text = (Sat1Out - Sat1In).TotalHours.ToString("f2");
    //Sunday
    TimeSpan Sun1In, Sun1Out;
    if (!TimeSpan.TryParse(TextBoxInSun1.Text, out Sun1In)) Sun1In = default(TimeSpan);
    if (!TimeSpan.TryParse(TextBoxOutSun1.Text, out Sun1Out)) Sun1Out = default(TimeSpan);
    SunLabel1.Text = (Sun1Out - Sun1In).TotalHours.ToString("f2");



    //Week 2 Calculations
    //Monday
    TimeSpan Mon2In, Mon2Out;
    if (!TimeSpan.TryParse(TextBoxInMon2.Text, out Mon2In)) Mon2In = default(TimeSpan);
    if (!TimeSpan.TryParse(TextBoxOutMon2.Text, out Mon2Out)) Mon2Out = default(TimeSpan);
    MonLabel2.Text = (Mon2Out - Mon2In).TotalHours.ToString("f2");
    //Tuesday
    TimeSpan Tues2In, Tues2Out;
    if (!TimeSpan.TryParse(TextBoxInTues2.Text, out Tues2In)) Tues2In = default(TimeSpan);
    if (!TimeSpan.TryParse(TextBoxOutTues2.Text, out Tues2Out)) Tues2Out = default(TimeSpan);
    TuesLabel2.Text = (Tues2Out - Tues2In).TotalHours.ToString("f2");
    //Wednesday
    TimeSpan Wed2In, Wed2Out;
    if (!TimeSpan.TryParse(TextBoxInWed2.Text, out Wed2In)) Wed2In = default(TimeSpan);
    if (!TimeSpan.TryParse(TextBoxOutWed2.Text, out Wed2Out)) Wed2Out = default(TimeSpan);
    WedLabel2.Text = (Wed2Out - Wed2In).TotalHours.ToString("f2");
    //Thursday
    TimeSpan Thurs2In, Thurs2Out;
    if (!TimeSpan.TryParse(TextBoxInThurs2.Text, out Thurs2In)) Thurs2In = default(TimeSpan);
    if (!TimeSpan.TryParse(TextBoxOutThurs2.Text, out Thurs2Out)) Thurs2Out = default(TimeSpan);
    ThursLabel2.Text = (Thurs2Out - Thurs2In).TotalHours.ToString("f2");
    //Friday
    TimeSpan Fri2In, Fri2Out;
    if (!TimeSpan.TryParse(TextBoxInFri2.Text, out Fri2In)) Fri2In = default(TimeSpan);
    if (!TimeSpan.TryParse(TextBoxOutFri2.Text, out Fri2Out)) Fri2Out = default(TimeSpan);
    FriLabel2.Text = (Fri2Out - Fri2In).TotalHours.ToString("f2");
    //Saturday
    TimeSpan Sat2In, Sat2Out;
    if (!TimeSpan.TryParse(TextBoxInSat2.Text, out Sat2In)) Sat2In = default(TimeSpan);
    if (!TimeSpan.TryParse(TextBoxOutSat2.Text, out Sat2Out)) Sat2Out = default(TimeSpan);
    SatLabel2.Text = (Sat2Out - Sat2In).TotalHours.ToString("f2");
    //Sunday
    TimeSpan Sun2In, Sun2Out;
    if (!TimeSpan.TryParse(TextBoxInSun2.Text, out Sun2In)) Sun2In = default(TimeSpan);
    if (!TimeSpan.TryParse(TextBoxOutSun2.Text, out Sun2Out)) Sun2Out = default(TimeSpan);
    SunLabel2.Text = (Sun2Out - Sun2In).TotalHours.ToString("f2");

    //Total hours for the two weeks
    TimeSpan Totals;
    //if (!TimeSpan.TryParse(TextBoxInMon1.Text, out Totals)) Totals = default(TimeSpan);
    //TotalHoursLabel.Text = (Totals).TotalHours.ToString("f2");
}

1 个答案:

答案 0 :(得分:3)

这是一个提示......

private TimeSpan CalculateDay(TextBox txtIn, TextBox txtOut, Label lbl) {
    TimeSpan timein, timeout;
    if (!TimeSpan.TryParse(txtIn.Text, out timein)) 
        timein = default(TimeSpan);
    if (!TimeSpan.TryParse(txtOut.Text, out timeout)) 
        timeout = default(TimeSpan);
    lbl.Text = (timeout - timein).TotalHours.ToString("f2");
    return timeout-timein;
} 

TimeSpan total = CalculateDay(TextBoxInSat1, TextBoxOutSat1, SatLabel1) +  CalculateDay(TextBoxInSun1, TextBoxOutSun1, SunLabel1) +  ...