[英]Divide timespan by 2?
I have two times, and their values are picked up from a XML from web. 我有两次,他们的价值观来自网络上的XML。
XElement xmlWdata = XElement.Parse(e.Result);
string SunRise = xmlWdata.Element("sun").Attribute("rise").Value;
string SunSet = xmlWdata.Element("sun").Attribute("set").Value;
DateTime sunrise = Convert.ToDateTime(SunRise.Remove(0,11));
DateTime sunset = Convert.ToDateTime(SunSet.Remove(0, 11));
This gives med the time: 04:28 for sunrise, and 22:00 for sunset. 这给了时间:日出时间是04:28,日落时间是22:00。 How to then do a calculation where i take:
然后如何进行计算:
(sunrise + (sunset-sunrise)/2)
I think you want to do this: 我想你想这样做:
TimeSpan span = sunset-sunrise;
TimeSpan half = new TimeSpan(span.Ticks / 2);
DateTime result = sunrise + half;
It can be written in one line if you want. 如果你愿意,它可以写成一行。
TimeSpan sunnyTime = TimeSpan.FromTick(sunrise.Ticks + (sunset.Ticks - sunrise.Ticks) / 2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.