-1

I'm working with timestamps in the form of [(h13m30){h4m30}] Right now i've gotten to the point where I have "13.30" and "4.30" in string format. I now need to combine these strings in such a way that 4.30 is added to 13.30, resulting in a string with value "18.00". However, this adding needs to comply with the 24h clock (e.g 23.00 + 4.00 would need to result in 03.00, not 27.00).

I can use DateTime.Add to add the 4.30 to 13.30, but i'm not sure how to convert the string "13.30" to a DateTime format. Could you help me out?

miradulo
  • 28,857
  • 6
  • 80
  • 93
KDE
  • 65
  • 5
  • Can you please create a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) for your problem? – Soner Gönül Jan 18 '16 at 14:14

1 Answers1

4

Use the TimeSpan struct:

TimeSpan ts1 = TimeSpan.ParseExact("13.30", "h\\.mm", DateTimeFormatInfo.InvariantInfo);
TimeSpan ts2 = TimeSpan.ParseExact("4.30", "h\\.mm", DateTimeFormatInfo.InvariantInfo);
TimeSpan tsResult = ts1 + ts2;
string result = tsResult.ToString("hh\\.mm");  // 18.00
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939