Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have one table returndate and actualreturndate i want to calculate days b/w them without saving in databas
Posted

Try this Query



Select DATEDIFF(DAY,ReturnDate,ActualRe
SQL

turndate) As FineDays From BookIssueTable
 
Share this answer
 
hi...
I guess that u need it in SQL server and not in C# code behind.

try this: (u need to set dates in variables from ur table)

SQL
declare @date1 datetime
declare @date2 datetime

set @date1='2012-09-12'
set @date2='2012-09-15'

select DATEDIFF(d,@date1,@date2) as 'Difference'


Result: 3
 
Share this answer
 
Comments
Mandy Nagra 15-Sep-12 2:40am    
i dunt want to use database.... only coding in visual
then use like this:

C#
DateTime date1 = DateTime.Parse("2012-09-12");
DateTime date2 = DateTime.Parse("2012-09-15");

TimeSpan ts = date2.Subtract(date1);

lblDifference.Text = ts.TotalDays.ToString();


:)
 
Share this answer
 
v3
Comments
Mandy Nagra 15-Sep-12 3:15am    
if we hv different dates in same table and we want to calculate dat not fixed dates then
Gautam Raithatha 15-Sep-12 3:23am    
then u can select dates from those tables by your data access method and assign them to 'date1' and 'date2' variables in above example. :)
Mandy Nagra 15-Sep-12 3:30am    
lbldiference id date given in gridveiw name to label
Gautam Raithatha 15-Sep-12 3:37am    
i cant understand what u said?
Mandy Nagra 15-Sep-12 3:42am    
i have bookssiue table in which ActualReturndate and ReturnDate r thr nw m making from i want to Calculate difference in days without Add value of fine days in database
Thats what i was telling. Fetch data from database into DataTable using your access method:

DataTable, say dt, looks something like:

-------------------------------------------
BookIssueId | ReturnDate | ActualReturnDate
-------------------------------------------
1           | 2012-09-12 | 2012-09-15
2           | 2012-09-05 | 2012-09-07
-------------------------------------------


Then add column "DifferenceDays" in dt:

C#
dt.Columns.Add("DifferenceDays", typeof(int));


Then calculate difference days as below:

C#
foreach (DataRow dr in dt.Rows)
        {
            DateTime date1 = DateTime.Parse(dr["ReturnDate"].ToString());
            DateTime date2 = DateTime.Parse(dr["ActualReturnDate"].ToString());

            TimeSpan ts = date2.Subtract(date1);

            dr["DifferenceDays"] = ts.TotalDays.ToString();
        }

Now set dt as grids datasource. Thats it!!! :)
 
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