多次添加(HH.mm)

时间:2017-11-04 05:47:41

标签: c# asp.net .net

sqlserver中的数据 在gridview中调用它 如何在列中添加所有时间,我有4列来计算不同的总时间 例如:
时间1 = 256.20(HH.mm)
时间2 = 257.41(HH.mm)
时间3 = 114.50(HH.mm)
时间4 = 371.20(HH.mm)
总计= 1000.11(HH.mm)

总数将继续增加,但格式必须是小时和分钟 我使用字符串拆分在上面添加了2次时间1 在C#.net

中完成

提前感谢你。

非常感谢@CodingYoshi

>        //string time1 = TextBox1.Text;
         //string time2 = TextBox2.Text;

        //string[] part1 = time1.Split('.');
        //int h1 = int.Parse(part1[0]);
        //int m1 = int.Parse(part1[1]);

        //string[] part2 = time2.Split('.');
        //int h2 = int.Parse(part2[0]);
        //int m2 = int.Parse(part2[1]);

        //int totalHours = h1 + h2;
        //int totalMinutes = m1 + m2;

        //if (totalMinutes >= 60)
        //{
        //    totalHours = totalHours + 1;
        //    totalMinutes = totalMinutes % 60;
        //}

        var times = new List<string>
        {
            TextBox1.Text,TextBox2.Text
        };

        TimeSpan total = new TimeSpan();
        foreach (var thisString in times)
        {
            var split = thisString.Split('.');
            total = total.Add(new TimeSpan(int.Parse(split[0]), int.Parse(split[1]), 0));
        }

        var totalHours = (int)total.TotalHours;
        var totalMinutes = total.Minutes;

        TextBox3.Text = totalHours.ToString() + "." + totalMinutes.ToString();
来自gridview的

SqlCommand cmd = con.CreateCommand();
        cmd.CommandText = "select * from totalHours";
        SqlDataReader dr = cmd.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Load(dr);
        GridView1.DataSource = dt;
        GridView1.DataBind();

    TimeSpan total = new TimeSpan();

    var times = new List<string> ();

    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        times.Add((GridView1.Rows[i].Cells[0].Text));
    }

    foreach (var thisString in times)
    {
        var split = thisString.Split('.');
        total = total.Add(new TimeSpan(int.Parse(split[0]), int.Parse(split[1]), 0));
    }

    var totalHours = (int)total.TotalHours;
    var totalMins = total.Minutes;

    Label2.Text = totalHours.ToString() + "." + totalMins.ToString();

再次感谢@CodingYoshi

1 个答案:

答案 0 :(得分:2)

我在这里添加其中的两个,你可以完成其余的工作:

<input class="text" type="text" name="name" value="<?= $username ?>" /> 

@posts = Post.limit(params[:limit]) 会显示添加var t1 = new TimeSpan(256, 20, 0); var t2 = new TimeSpan(257, 41, 0); var tTotal = t1.Add(t2).ToString(@"dd\.hh\:mm\:ss"); tTotal的日期,时间,分钟和秒数;

如果你想使用循环来完成它们,你可以这样做:

t1

如果您想要总小时数和分钟数:

t2