Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have 2 Textbox on my form. First box for enter input time and second for enter output time.
For example 7:30 in textbox1 and 12:25 in textbox2.

1- In the first ,in SQL Server DB table, what type must be select for these 2 time fields?
There is a type datetime but I wants save only time not date.

2- How can I calculate difference between 2 time fields? following commands is wrong:
DateTime dt1 = new DateTime();
dt1 = Convert.ToDateTime(textBox1.Text) - Convert.ToDateTime(textBox1.Text);

Thanks very much
Posted

  1. DATE.
  2. C#
    System.DateTime earlier = //...
    System.DateTime later = //...
    System.TimeSpan duration = later - earlier;
    // even if earlier > later, by some reason, negative time spans are also acceptable



—SA
 
Share this answer
 
v2
HI

1. which version of SQL SERVER do you use ? in SQL SERVER 2008 R2 there is time type which can store timespan, there is datetime and datetime2 for store date and time

but no matter if you cannot use the time type you can store your time in the datetime type field and then when you retrive this value from your database you take only the time part and work with

VB
Dim objDate As DateTime = CDate( datarow("DateTimeField"))
Dim objTime As String =  objDate.ToShortTimeString()



2. for calculate the difference between two time field you can use TimeSpan or DateDiff like this

VB
Dim timeDiff As Long = DateDiff(DateInterval.Hour, objDate1, objDate2)


or

VB
Dim objDate1 As DateTime = Date.Now()
Threading.Thread.Sleep(3000)
Dim objDate2 As DateTime = Date.Now()
Dim span As TimeSpan = objDate2 - objDate1
Console.WriteLine(span.TotalSeconds)
 
Share this answer
 
v2
Comments
phil.o 1-Dec-13 17:50pm    
As Sergey stated in solution 2, TimeSpan structure allows to handle operations between DateTimes without the hassle of converting.
Much more efficient and intuitive :)
Jean-Claude ADIBA 2-Dec-13 2:02am    
Have you read my answer with his logic ? Solution 1 use TimeSpan and i show it. but he post his question in VB.NET Section so i wrote the answer in VB.net and i show the Existence of DateDiff (Dim timeDiff As Long = DateDiff(DateInterval.Hour, objDate1, objDate2)
).

He ask 2 Questions i try to give answer for these 2 question but Sergey no so ...
My answer don't need conversion any time. i show ealer how to get the part of time in the date as string (he uses a textbox) : Dim objTime As String = objDate.ToShortTimeString()
after i give how have difference between two date with TimeSpan

Dim objDate1 As DateTime = Date.Now()
Threading.Thread.Sleep(3000)
Dim objDate2 As DateTime = Date.Now()
Dim span As TimeSpan = objDate2 - objDate1
Console.WriteLine(span.TotalSeconds)

phil.o 2-Dec-13 4:06am    
I read your solution, yes; what I can see now is that it has been modified and does not use any conversion anymore.
Jean-Claude ADIBA 2-Dec-13 5:38am    
I desagree I'm sorry, i inform you that after your post i don't edit the solution
you can see the editions history (http://www.codeproject.com/script/Articles/ListVersions.aspx?aid=690513)
yes i have edit just 20mn after my first publish jusT to add some precision but your comment was send later. i accept to be correct but please read before comment and downvote
You can follow the bellow code to subtract time as an example:


C#
DateTime dt = DateTime.Now;
DateTime dt2 = Convert.ToDateTime("1/2/2013 20:30");
TimeSpan ts = (dt2 - dt);
Response.Write(ts.ToString("hh\\:mm\\:ss"));


-Thanks,
Mamun
 
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