i have two times in the format.."hours:mins:secs" and and I want to add these two times..how can I do that in C#.....for example...i have two different times like...."2:10:15" and "1:10:55"...the result should be "3:21:10"..can anyone tell me how to do this???
-
2What does this have to do with Excel?Blorgbeard– Blorgbeard2011-02-26 08:47:30 +00:00Commented Feb 26, 2011 at 8:47
-
those two timings in excel...Kumar– Kumar2011-02-26 08:56:42 +00:00Commented Feb 26, 2011 at 8:56
-
I want to add two time by c#.nte coding and pass it to excel..Kumar– Kumar2011-02-26 08:57:17 +00:00Commented Feb 26, 2011 at 8:57
-
You need to describe the problem more clearly. How is this in any way related to Excel? "pass it to excel" is far too simple a description.Lasse V. Karlsen– Lasse V. Karlsen2011-02-26 09:30:02 +00:00Commented Feb 26, 2011 at 9:30
3 Answers
This should do it, like Dean suggested:
var t1 = TimeSpan.Parse("2:10:15");
var t2 = TimeSpan.Parse("1:10:55");
var result = t1 + t2;
3 Comments
While I don't know about datetime conversions between Excel and .Net data types, from looking at your question I wonder if you understand Excel's time values vs the formats. If you're returning a string "2:10:15" then you can just parse it to a datetime value. But if you're reading that number from a cell then the actual value is a floating point number, for example 0.0876 - the value to the right of the decimal being the fraction of the day since midnight. So you should just be able to assign the values from Excel to double variables and perform straight math on them, taking care to handle times that are on separate days.
Sorry I can't be more explicit but I'm just posting from my ipad so can't really give you an example.