Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 19-Jul-11 20:00pm
AC777169
Edited 19-Jul-11 20:02pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

hello,
 
try to use use TimeSpan or search TimeSpan example.
 
thanks
sanjeev
  Permalink  
Comments
walterhevedeich at 20-Jul-11 2:14am
   
Good advice. 5ed.
SanjeevSingh at 20-Jul-11 2:42am
   
thanks
Dalek Dave at 21-Jul-11 3:51am
   
Good Call!
Rate this: bad
good
Please Sign up or sign in to vote.

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);
  Permalink  
Comments
walterhevedeich at 20-Jul-11 2:15am
   
Right. 5ed.
alavee1 at 18-Aug-13 23:38pm
   
Simple and clear solution.
Thanks Abhinav S.
Abhinav S at 20-Jul-11 2:16am
   
Thank you.
SAKryukov at 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 at 21-Jul-11 3:56am
   
Thanks for the 5 SA.
Rate this: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
Comments
Dalek Dave at 21-Jul-11 3:52am
   
Explain why better?
Serious question, functionally they are identical.
SAKryukov at 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 at 21-Jul-11 4:01am
   
Curiosity - How will it be better?
SAKryukov at 21-Jul-11 4:04am
   
I though it's apparent. I explained above.
--SA
Nagy Vilmos at 21-Jul-11 4:34am
   
If it's an 'improvement' why didn't you just improve the answer instead of creating another answer?
SAKryukov at 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 at 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 at 18-Dec-12 2:00am
   
I agree with you Nagy on this matter.
Sergey Alexandrovich Kryukov at 18-Dec-12 4:18am
   
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 at 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.
Sergey Alexandrovich Kryukov at 18-Dec-12 4:33am
   
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: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
v2

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



Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid