c# - 如何以 DateTime 格式初始化 8 小时

标签 c#

我想以 DateTime 格式初始化 8 小时。我试过这样

DateTime stayingTime = 8;
TimeSpan span = (stayingTime-runningTime);

我将从其他来源获取runningTime。我想用 8 小时减去 runningTime 并以以下格式显示答案 hh:mm

最佳答案

您正在寻找 TimeSpan-structure :

TimeSpan stayingTime = TimeSpan.FromHours(8);
TimeSpan span = stayingTime - runningTime;

如果您需要它作为DateTime,您可以使用TimeOfDay property :

DateTime stayingTime = new DateTime().AddHours(8);
TimeSpan span = stayingTime.TimeOfday - runningTime;

如果你想用hh:mm格式输出它,使用TimeSpan.ToString :

span.ToString("hh':'mm")

关于c# - 如何以 DateTime 格式初始化 8 小时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33392404/

相关文章:

c# - XmlDocument SelectNodes(Xpath): Order of result

c# - X509Certificate2.验证行为

c# - 手动回发在 asp.net 中不起作用

c# - 带委托(delegate)的事件处理程序

c# - 如何确定 Xamarin Forms 中水龙头的位置?

c# - 一个类上有很多方法会增加该类对象的开销吗?

C# 将 'old' WinForms 转换为 Web 应用程序,还是在新平台上重写?

c# - Telerik RadGrid-向 GridEditCommandColumn 添加 javascript 方法

c# - 如何返回包含在 1 个函数中找到的总行数的记录

c# - 从 ASP.Net Webservice 获取超时