如何在C#中计算两个不同的时间

编程入门 行业动态 更新时间:2024-10-15 06:18:20
本文介绍了如何在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:00

refer below link stackoverflow/questions/510778/add-or-sum-of-hours-like-1330000020-133020-but-how[^]

更多推荐

如何在C#中计算两个不同的时间

本文发布于:2023-11-21 23:38:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615122.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   时间   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!