如何增加一天的额外时间

编程入门 行业动态 更新时间:2024-10-24 20:23:13
本文介绍了如何增加一天的额外时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好朋友我有这个问题

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;

更多推荐

如何增加一天的额外时间

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

发布评论

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

>www.elefans.com

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