Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,

I have working on asp.net with c#, In I want to create one small module which related attendance and overtime.
Means, One man enter with that datetime and also leave with that datetime,

like Enter Date: 15/12/2014 21:00
Leave Date: 16/12/2014 11:00

so, I want to know, he has worked how many hours ?, with above datetime.
By manually he has worked around 14 Hours. but i want calculate with C#.



Thanks & Regards.
Posted
Comments
Thanks7872 15-Dec-14 4:39am    
You know there is something called timespan. Then what have you tried your self? Any efforts?
Pratham4950 15-Dec-14 4:57am    
Yes, I have use like this.
string enter = "15/12/2014 21:00";
string leave = "16/12/2014 11:00";
TimeSpan dt1 = TimeSpan.Parse(enter.ToString());
TimeSpan dt2 = TimeSpan.Parse(leave.ToString());
TimeSpan tsWorkHours = dt2 - dt1 ;

But, with this "String was not recognized as a valid TimeSpan."
this error is occurred on "TimeSpan dt1 = TimeSpan.Parse(enter.ToString());" this line..
Thanks7872 15-Dec-14 5:14am    
Then you were suppose to explain this story while posting question. Never assume that we can read your mind.
Pratham4950 15-Dec-14 5:22am    
Right, Now you have any idea for that ?
Richard MacCutchan 15-Dec-14 6:19am    
Why are you calling ToString on a string variable?

Use a TimeSpan[^].
 
Share this answer
 
Something like:
C#
string enter = "15/12/2014 21:00";
string leave = "16/12/2014 11:00";
DateTime dtEnter = DateTime.Parse(enter);
DateTime dtLeave = DateTime.Parse(leave);
TimeSpan tsWorkHours = dtLeave - dtEnter;
Console.WriteLine("Work hours {0}", tsWorkHours.Hours);
 
Share this answer
 
Comments
Pratham4950 15-Dec-14 4:48am    
@CPallini Yes, something like this, but when i have trying like this that time
"String was not recognized as a valid DateTime" this error is occurred on this
"DateTime dtEnter = DateTime.Parse(enter);" line..
lukeer 15-Dec-14 6:19am    
Use DateTime.TryParseExact()[^] instead of a simple DateTime.Parse()[^]. That way, you can specify the exact (hence the method name) structure of the date that gets entered. That helps to handle cultural differences in writing a date.
TheRealSteveJudge 15-Dec-14 7:13am    
You did not ignore the fact that the dates were given as strings. 5*
You must use DateTime.Subtract() method.
Then you must get the TotalHours property of the TimeSpan.

C#
DateTime enterDate = new DateTime(2014, 12, 15, 21, 0, 0);
DateTime leaveDate = new DateTime(2014, 12, 16, 11, 0, 0);

TimeSpan duration = leaveDate.Subtract(enterDate);

double workingHours = duration.TotalHours;
 
Share this answer
 
v2
Comments
TheRealSteveJudge 15-Dec-14 5:49am    
WTF is wrong with this solution? Somebody voted it down. Thank you!
lukeer 15-Dec-14 6:14am    
Nothing needs to be wrong in a solution for it to get downvoted. That just happens.
Richard MacCutchan 15-Dec-14 6:21am    
Probably because you ignored the fact that OP has specified that his time values are stored as strings. The grammar police are everywhere.
CPallini 15-Dec-14 7:00am    
Have my 5 for balancing.
TheRealSteveJudge 15-Dec-14 7:12am    
Thank you!

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