34

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

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)

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Megaoctane
  • 919
  • 3
  • 9
  • 10
  • 6
    Most of the code here has nothing to do with the question. It is OK to ask a very short question. – usr May 20 '12 at 09:18

2 Answers2

75

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.

Casperah
  • 4,504
  • 1
  • 19
  • 13
1

TimeSpan sunnyTime = TimeSpan.FromTick(sunrise.Ticks + (sunset.Ticks - sunrise.Ticks) / 2);

qwertyuu
  • 11
  • 1