开发者

C#.Net Excel Reading and Writing

开发者 https://www.devze.com 2023-02-13 08:27 出处:网络
i have two times in the format..\"hours:mins:secs\" and and I want to add these two time开发者_JAVA技巧s..how can I do that in C#.....for example...i have two different times like....\"2:10:15\" and \

i have two times in the format.."hours:mins:secs" and and I want to add these two time开发者_JAVA技巧s..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???


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;


You'll want to declare two timespan variables plugging in the values above. This will allow you to then add the two time together


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消