Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?

please help me.

Thanks in Advance.
Posted
Updated 8-Jul-18 22:06pm
v2

hello,

try to use use TimeSpan or search TimeSpan example.

thanks
sanjeev
 
Share this answer
 
Comments
walterhevedeich 20-Jul-11 2:14am    
Good advice. 5ed.
[no name] 20-Jul-11 2:42am    
thanks
Dalek Dave 21-Jul-11 3:51am    
Good Call!
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);
 
Share this answer
 
Comments
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.
Sergey Alexandrovich Kryukov 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.
One improvement: in the solution by Abhinav, it's much better to write the last line like this:

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


—SA
 
Share this answer
 
Comments
Dalek Dave 21-Jul-11 3:52am    
Explain why better?
Serious question, functionally they are identical.
Sergey Alexandrovich Kryukov 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?
Sergey Alexandrovich Kryukov 21-Jul-11 4:04am    
I though it's apparent. I explained above.
--SA
Nagy Vilmos 21-Jul-11 4:34am    
If it's an 'improvement' why didn't you just improve the answer instead of creating another answer?
Without using any SqlFunction

C#
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900