Click here to Skip to main content
15,887,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In database i have two field called In1 and Out1 datatype as varchar(50)

In1 = 9:30 AM
Out1 = 11:00 Am

i have to calculate difference of above two. how to do it??

please help me
Posted
Comments
CHill60 9-Jul-13 16:16pm    
A suggestion to add to the solutions below - store dates on your database as DateTime or Date column types in preference to varchars - you can take advantage of database-side calculations and avoid the pitfalls of date formatting (especially across cultures)

You can use timespan[^] to calculate time diff in c#.
Have a look:
C#
DateTime dt1=Textbox1.Text; 
DateTime dt2=Textbox2.Text; 

TimeSpan t=dt2.Subtract(dt1);

string diff = t.Hours.ToString()+":"+t.Minutes.ToString() + ":" + t.Seconds.ToString();
 
Share this answer
 
Comments
ridoy 21-Dec-12 2:29am    
+5
Prasad_Kulkarni 21-Dec-12 2:36am    
Thanks Ridoy!
Hi,

Try below code.
C#
DateTime dt1 = DateTime.ParseExact(In1, "HH:mm:ss", new DateTimeFormatInfo());
DateTime dt2 = DateTime.ParseExact(Out1, "HH:mm:ss", new DateTimeFormatInfo());
TimeSpan ts = dt1.Subtract(dt2);

For more info refer the below link.
http://www.dotnetspider.com/forum/217151-Time-difference-c.aspx[^]
 
Share this answer
 
v2
C#
DateTime in1 = DateTime.Parse(dt_display.Rows[i]["In1"].ToString());
                    DateTime out1 = DateTime.Parse(dt_display.Rows[i]["Out1"].ToString());
                    TimeSpan diff = out1.Subtract(in1);
 
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