DateTime Difference

gokuldas
 
on Jan 18, 2013 09:54 PM
3670 Views

In sql server 2005 , I have a database say history and table record history which as column A which is stored as DateTime(Date and Time gets stored), column Time which is stored as char(Time only gets stored). In column C I need Time difference between column A and Column B whenever I update Time (coloum) in visual studio 2010 using c# code.

How will I get the Time difference. Since one is in dateTime format and in other only Time gets stored up in char? Please give the code. I tried but got error

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Vasanth
 
on Jan 18, 2013 10:30 PM

In the column "Time," in the table, it stores the current time or todays time?

Can u please tell  time of which date is stored in the "Time" Column in the table.

gokuldas
 
on Jan 18, 2013 10:40 PM

The time is stored which the user wants to store say 09:00:00 am.

The time is stored to record based on the user request by say admin. its not the current date or today's time, the time of a particular day as to be updated

Vasanth
 
on Jan 18, 2013 10:48 PM

Okay, As for as i  understand i can give u some logic.

  - Convert Char to time in sql. ()i.e your time field and store that in temp variable.

  - In the datetime field, u can seperate the time from the datetime and store that in another varible

 - Compare both the time is the result.

Converting Char to time and seperating the time from date time is stright forward and u can search for the code.

Please let me know if this is not clear.

 

gokuldas
 
on Jan 18, 2013 10:50 PM

Logic is correct I think,please give code as an example please

Mustafa
 
on Jan 19, 2013 01:44 AM

Ok Gokuldas you can try this

 DateTime ObjLocDate1 = new DateTime();
        DateTime ObjLocDate2 = new DateTime();
        string StrLocTime = string.Empty;
        ObjLocDate1 = Convert.ToDateTime(txtDate1.Text);
        ObjLocDate2 = Convert.ToDateTime(txtDate2.Text);
        StrLocTime = string.Format("{0}:{1}:{2}", ObjLocDate1.Subtract(ObjLocDate2).Hours.ToString(), ObjLocDate1.Subtract(ObjLocDate2).Minutes.ToString(), ObjLocDate1.Subtract(ObjLocDate2).Seconds.ToString());
        lblDate.Text = StrLocTime;

In this example i have simply passed the hard code values to the  textbox.

But in you case you will have to fetch the time from you table