如何添加两个文本框的时间间隔(hh:mm:ss)+(hh:mm:ss) [英] how to add the time intervals of two textboxes (hh:mm:ss)+(hh:mm:ss)
本文介绍了如何添加两个文本框的时间间隔(hh:mm:ss)+(hh:mm:ss)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何添加两个文本框的时间间隔(hh:mm:ss)+(hh:mm:ss)
how to add the time intervals of two textboxes (hh:mm:ss)+(hh:mm:ss)
推荐答案
假设您有两个带有以下值:
Assuming you have two TextBoxes with the following values:
TextBox1.Text = "12:34:45"
TextBox2.Text = "4:0:0"
,并且您希望将这些值解释为TimeSpans(时间长度).
And you intend that these values are interpreted as TimeSpans (lengths of times).
TimeSpan Span1 = TimeSpan.Parse(TextBox1.Text);
TimeSpan Span2 = TimeSpan.Parse(TextBox2.Text);
TimeSpan TotalTime = Span1 + Span2;
由于您输入的内容可能不正确,请参阅有关使用TimeSpan.TryParse方法的MSDN文档,该方法将使您进行测试输入字符串的有效性,以一种有用的方式.
Since you may have incorrect input, please see the MSDN docs on using the TimeSpan.TryParse method which will let you test the input string for validity in a useful way.
将它们转换为DateTime,然后使用这些部分来构建时间跨度.最后,将它们添加在一起.
Convert them to a DateTime, then use the parts to build a timespan. Finally, add them together.
string t1 = "01:02:03";
string t2 = "10:12:13";
DateTime dt1 = DateTime.Parse(t1);
DateTime dt2 = DateTime.Parse(t2);
TimeSpan ts1 = new TimeSpan(dt1.Hour, dt1.Minute, dt1.Second);
TimeSpan ts2 = new TimeSpan(dt2.Hour, dt2.Minute, dt2.Second);
TimeSpan sum = ts1 + ts2;
如果您是说在服务器端:
If you mean on server side :
DateTime F1 = DateTime.Parse(textbox1.Text);
F1 = F1 + TimeSpan.Parse(textbox2.Text);
如果您是在客户端使用,请阅读以下内容:
http://javascript.internet.com/time-date/add-time.html [ ^ ]
And if you mean on client side read this :
http://javascript.internet.com/time-date/add-time.html[^]
这篇关于如何添加两个文本框的时间间隔(hh:mm:ss)+(hh:mm:ss)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文