Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a windows form that gives me time_in and time_out for all the users. Time_in and time_out are also tables in my sql database. I'd like to find work hours which can be calculated as time_out - time_in. Time_in and time_out are time format (hh:mm:ss). How to find the work hour ? The code that I tried doesn't work becuase of the data variable type. But I couldn't do this with the int data type anyway.
string time_in = databaseTimeIn;
string time_out = databaseTimeOut;
string workhour = time_out-time_in;
Posted
Comments
Kornfeld Eliyahu Peter 8-Dec-14 13:31pm    
If you want to do calculations with time, than use type that stores the values AS time and not as string!!!

Don't use strings for date and time data! If you are using SQL Server, DATETIME values can be operated on directly, or use DATEDIFF.

http://msdn.microsoft.com/en-us/library/ms189794.aspx[^]
 
Share this answer
 
Comments
/\jmot 9-Dec-14 0:28am    
+5,
hi,

to calculate Work Hours you need to convert in_time and out_time in HH format so use the formula in_time as HH+mm/60+ss/60/60 i.e 07:23:55-->>07+23/60+55/60/60=7.39 hrs

after that calculate Work Hours as diff of out-in
 
Share this answer
 
use

DATEDIFF(datepart,startdate,enddate)
eg: SELECT DATEDIFF(day,'2008-06-05','2008-08-05') AS DiffDate

ans:61
 
Share this answer
 

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