本文介绍了如何在C#中计算两个不同的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何计算C#中的两个不同时间(窗口) Ex- 时间1 - 11:10:40 时间2 - 00:12:10 总和 - 11:22:50 提前感谢
解决方案如果你的意思是添加两个日期,这将有助于 System.DateTime today = System.DateTime.Now ; System.TimeSpan duration = new System.TimeSpan( 36 , 0 , 0 , 0 ); System.DateTime answer = today.Add(duration); System.Console.WriteLine( {0:dddd},answer);
您可以在以下链接中找到更多相关信息 TimeSpan Structure [ ^ ] DateTime.Add方法 [ ^ ]
如果你选择使用TimeSpan,要注意Days部分: TimeSpan t1 = TimeSpan.Parse( 23:30); TimeSpan t2 = TimeSpan.Parse( 00:40:00); TimeSpan t3 = t1.Add(t2); Label1.Text = t3.ToString(); // 绑定到标签 Console.WriteLine(t3); // 1.00:10:00 使用DateTime: DateTime d1 = DateTime.Parse( 23:30); DateTime d2 = DateTime.Parse( 00:40:00); DateTime d3 = d1.Add(d2.TimeOfDay); Label2.Text = t3.ToString(); // 绑定到标签 Console.WriteLine(d3.TimeOfDay); // 00:10:00
请参考以下链接 stackoverflow/questions/510778/add-or-sum-of-hours-like-1330000020-133020-but-how [ ^ ]
How to calculate two different time in C# (windows) Ex- Time 1 - 11:10:40 Time 2 - 00:12:10 Sum - 11:22:50 Thank in advance
解决方案 If you mean adding two dates this will help System.DateTime today = System.DateTime.Now; System.TimeSpan duration = new System.TimeSpan(36, 0, 0, 0); System.DateTime answer = today.Add(duration); System.Console.WriteLine("{0:dddd}", answer);you can find more details about this in links below TimeSpan Structure[^] DateTime.Add Method [^]
If you choose to use the TimeSpan, be aware about the Days part: TimeSpan t1 = TimeSpan.Parse("23:30"); TimeSpan t2 = TimeSpan.Parse("00:40:00"); TimeSpan t3 = t1.Add(t2); Label1.Text = t3.ToString(); //to bind to label Console.WriteLine(t3); // 1.00:10:00 With DateTime: DateTime d1 = DateTime.Parse("23:30"); DateTime d2 = DateTime.Parse("00:40:00"); DateTime d3 = d1.Add(d2.TimeOfDay); Label2.Text = t3.ToString(); //to bind to label Console.WriteLine(d3.TimeOfDay); // 00:10:00refer below link stackoverflow/questions/510778/add-or-sum-of-hours-like-1330000020-133020-but-how[^]
更多推荐
如何在C#中计算两个不同的时间
发布评论