简体   繁体   English

如何将 TimeSpan 值的总和显示为 hh:mm:ss 格式的文本框

[英]How to display a sum of TimeSpan values into a textBox as hh:mm:ss format

I'm converting user-entered textBox values to TimeSpan and adding all TimeSpan values to get a "total amount of time" if you will.如果您愿意,我正在将用户输入的 textBox 值转换为 TimeSpan 并添加所有 TimeSpan 值以获得“总时间”。 I then need to display that total to another textBox in hh:mm:ss format.然后我需要以 hh:mm:ss 格式将该总数显示到另一个文本框。

Right now I have it so that is adds all the values correctly, however it just returns the TotalHours value to the string instead of hh:mm:ss format.现在我有了它,因此可以正确添加所有值,但是它只是将 TotalHours 值返回到字符串而不是 hh:mm:ss 格式。

    private void calculate_btn_Click(object sender, EventArgs e)
    {

        TimeSpan d1 = TimeSpan.Parse(textBox1.Text);
        TimeSpan d2 = TimeSpan.Parse(textBox2.Text);
        TimeSpan d3 = TimeSpan.Parse(textBox3.Text);

        TimeSpan total = d1 + d2 + d3;

        totaltimebox.Text = (total.TotalHours).ToString();

    }

User enters 00:10:00 and 00:09:00.用户输入 00:10:00 和 00:09:00。

I expect the output to be 00:19:00, but the current output is 19.我希望输出为 00:19:00,但当前输出为 19。

Try totaltimebox.Text= $"{total.Hours}:{total.Minutes}:{total.Seconds}";试试totaltimebox.Text= $"{total.Hours}:{total.Minutes}:{total.Seconds}"; . .

It looks like what you are doing is returning the total hours, what you want to do it get the hours, minutes and seconds and singular values from the time span.看起来您正在做的是返回总小时数,您想要做的是从时间跨度中获取小时、分钟和秒以及奇异值。

Say you had a TimeSpan of 2 days, 2 hours, 20 minutes and 10 seconds, the return value of total.TotalHours would = 50. Whereas you actually want the individual components (the hours, the minutes and the seconds) and so using total.Hours would return 2. I hope this helps.假设您的时间跨度为 2 天、2 小时、20 分钟和 10 秒, total.TotalHours的返回值将 = 50。而您实际上想要单个组件(小时、分钟和秒),因此使用total.Hours将返回 2。我希望这会total.Hours帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM