12,634,788 members (31,969 online)
Rate this:
See more:
Hi All,

I have to write a function for calculating the time difference between two tasks entered by the same user which is to be displayed in the excel sheet.How to write that function?

Posted 19-Jul-11 21:00pm
AC777169
Updated 19-Jul-11 21:02pm
v2

Rate this:

## Solution 1

hello,

try to use use TimeSpan or search TimeSpan example.

thanks
sanjeev
walterhevedeich 20-Jul-11 2:14am

SanjeevSingh 20-Jul-11 2:42am

thanks
Dalek Dave 21-Jul-11 3:51am

Good Call!
Rate this:

## Solution 2

Try
```DateTime dt1 = DateTime.ParseExact(time1, "HH:mm:ss", new DateTimeFormatInfo());
DateTime dt2 = DateTime.ParseExact(time2, "HH:mm:ss", new DateTimeFormatInfo());
TimeSpan ts = dt1.Subtract(dt2);```
walterhevedeich 20-Jul-11 2:15am

Right. 5ed.
alavee1 18-Aug-13 23:38pm

Simple and clear solution.
Thanks Abhinav S.
Abhinav S 20-Jul-11 2:16am

Thank you.
SAKryukov 21-Jul-11 3:44am

Correct, my 5, but there is an improvement, please see my solution. Isn't it a bit more clear?
--SA
Abhinav S 21-Jul-11 3:56am

Thanks for the 5 SA.
Rate this:

## Solution 3

One improvement: in the solution by Abhinav, it's much better to write the last line like this:

```TimeSpan ts = dt1 - dt2;
//yes, subtraction operator "-" is defined, of course
```

—SA
Dalek Dave 21-Jul-11 3:52am

Explain why better?
Serious question, functionally they are identical.
SAKryukov 21-Jul-11 4:03am

Functionality? How about maintainability? Apparently, more readable, less confusion what is subtracted from what, no need to remember method name... Operators does not have to be defined from the pure functional stand point, but they are defined -- for good reasons.
Did I explained enough?
--SA
Abhinav S 21-Jul-11 4:01am

Curiosity - How will it be better?
SAKryukov 21-Jul-11 4:04am

I though it's apparent. I explained above.
--SA
Nagy Vilmos 21-Jul-11 4:34am

SAKryukov 21-Jul-11 10:22am

Why not? We always do so. Improvement is quite apparent; I don't see what problem do you see. Just decided to down-vote on this base? Hm...

--SA
Nagy Vilmos 21-Jul-11 10:27am

0. I didn't vote.
1. Your solution is better /from your point of view/, but that does not mean it is better for every one.
jibesh 18-Dec-12 2:00am

I agree with you Nagy on this matter.

You guys never gave me rational arguments. I explained why it's better, and you did not provide any arguments against my claim. "Same functionality" is true but not an argument. And "1." seems ridiculous to me. Of course, if I have a strong opinion, I try to share it. To say it's not better, you need to explain just one one example when it is not better and why.

Thank you.
—SA
jibesh 18-Dec-12 4:28am

Sergey this solution never deserve '1' , but this solution is same the other solution by all means. so what other members feel is its good to add to amend this result to other solution. I believe the primary objective of this site is to encourage each other to build a better programming community, develop good programmer etc etc and not merely for points. I do value each members contribution to this site with due respect.

I'm not arguing about the form of the post and even about the votes, not even close. I'm talking only about the coding style and related matters. Why would you care that I post a separate solution, really? Give me the rational arguments against my view of the usage of the operator. If you don't have objections, let's close it.
—SA
Rate this:

## Solution 4

Without using any SqlFunction

```var grandTotalWork = (from t in db.AssignmentHandymandetails join ad in db.AssignmentDetails on t.assignment_detail_id equals ad.assignment_detail_id where ad.assignment_id == Convert.ToInt32(Label_assignmentId.Text) select new { startTime = t.started_time.Value.TimeOfDay, endTime = t.end_time.Value.TimeOfDay, TotalWorkTime = (t.started_time.Value.TimeOfDay.Duration() - t.end_time.Value.TimeOfDay.Duration()).Duration()});
```

then you can bind the result in GridView you wish
v2

Top Experts
Last 24hrsThis month
 Richard MacCutchan 186 CPallini 175 OriginalGriff 128 F-ES Sitecore 65 ppolymorphe 59
 OriginalGriff 2,412 ppolymorphe 1,311 Peter Leow 1,029 CPallini 806 John Simmons / outlaw programmer 770