views:

61

answers:

2

I need to do a calculation for my crystal report.

In report, there will be Times(in Hour:Min:Sec)like this..

00:12:34

00:52:12

23:19:56

and so on..

I need to add them together so that..results will be

Hr:min:ss

Hr may be more than 100..

but can't change to day

How to achieve that?

Thanks a lot..

+1  A: 

Look up the TimeSpan.Parse method like this.

var t1 = TimeSpan.Parse("00:12:34");
var t2 = TimeSpan.Parse("00:52:12");
var t3 = TimeSpan.Parse("23:19:56");

var result = t1 + t2 + t3

Console.WriteLine("Total time is {0} Hours {1} Mins {2} secs", 
result.Days * 24 + result.Hours, result.Minutes, results.Seconds);
Preet Sangha
good idea.. thanks for that..but in ur ans, can i combine the Days and hours and show them together as Hours..?like 1 Day, 1 Hr as 25 Hours??
william
yes you can get Days from `TimeSpan` object like `result.Days`
TheVillageIdiot
william: You want `(int)result.TotalHours`.
Gabe
May b I didn't make it very clear.. What I wanna ask is..I dun want days..just hours..according to above example,the results should b "24:24:42"not 1 day, "00:24:42"
william
Kirk Broadhurst
Yea.. that's a good one..tkz a lot..
william
updated thanks for the comments
Preet Sangha
A: 

It you are getting hh:mm:ss only then this will be easy:

hh x 60 x 60 + mm x 60 + ss

and then get hours, mins and seconds from it.

If you are getting DateTime object then you can get hours, mins and convert them to seconds and use same technique as above.

TheVillageIdiot
yea.. that's one idea..get the total sec of everything and add them up together..after that.. divided back to hour, min, second.I have tried that.. the coding is too long.. so just asking any better idea..??sth like shortcut?
william