本文介绍了如何增加一天的额外时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好朋友我有这个问题
hello frens i have this problem
DateTime arrtime = DateTime.Parse(txtarrivaltime.Text); DateTime endTime = DateTime.Now; TimeSpan span1 = endTime.Subtract(arrtime); TimeSpan span2 = span1.Duration(); string NumOfDays = span1.Days; string Shours = span2.Hours.ToString()+ ?;现在我要动态增加小时数,即24 *一些值,例如24 * 1或24 * 2或...如果NumOfDays大于0,则以小时为单位. 请提供代码
now i want to dynamically add hours i.e. 24* some value like 24*1 or 24*2 or ... in Shours if the NumOfDays is more than 0. how can i do this dynamically? please provide with the code
推荐答案使用以下内容: Use the following : // add 1 day to the current date time DateTime dt = DateTime.Now.AddDays(1);
稍微转一下-做ToString最后,不要在字符串中存储要用于算术的任何内容: Turn it round a little - do the ToString last, and don''t store anything you want to use for arithmetic in a string: DateTime arrtime = DateTime.Parse("txtarrivaltime.Text"); DateTime endTime = DateTime.Now; TimeSpan span1 = endTime.Subtract(arrtime); TimeSpan span2 = span1.Duration(); int NumOfDays = span1.Days; TimeSpan span3 = new TimeSpan(24 * NumOfDays, 0, 0); TimeSpan span4 = span2.Add(span3); string Shours = span4.TotalHours.ToString();
(这是故意散布的,因此您可以更好地看到步骤)
(This is deliberately spread out, so you can see the steps better)
如果我正确理解了您的问题,您不能只添加span1.Days*24吗? 在任何情况下span1.Days可能为负吗?如果是这样,请使用+ span1.Days > 0 ? span1.Days * 24.0 : 0.0; If I undedrstand your question correctly, can''t you just add span1.Days*24? Is there any case where span1.Days might be negative? If so, then use + span1.Days > 0 ? span1.Days * 24.0 : 0.0;
更多推荐
如何增加一天的额外时间
发布评论