我想以DateTime格式初始化8小时。我试过这样
DateTime stayingTime = 8;
TimeSpan span = (stayingTime-runningTime);
我会从其他来源获得runningTime
。我想用8小时减去runningTime
,并按以下格式显示答案hh:mm
答案 0 :(得分:3)
您正在寻找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")