将时间跨度除以 2? [英] Divide timespan by 2?

查看:27
本文介绍了将时间跨度除以 2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两次,它们的值是从 Web 的 XML 中提取的.

I have two times, and their values are picked up from a XML from web.

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));

这给了 med 时间:日出时间为 04:28,日落时间为 22:00.然后如何进行计算:

This gives med the time: 04:28 for sunrise, and 22:00 for sunset. How to then do a calculation where i take:

(sunrise + (sunset-sunrise)/2)

推荐答案

我认为你想这样做:

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.

这篇关于将时间跨度除以 2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆