Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datediff getting the date inbetween 2 dates and bind it to a gridview

Tags:

c#

asp.net

I have a table with 'dateborrowed' and 'datereturned' column. What I want to do is I want to get the value in between 'datereturned' and 'dateborrowed' and bind it to another column in another table. Also how can I do it using datediff function? I'm still learning it in the meantime. Any help would be greatly appreciated.

Thanks in advance!!

like image 256
Loupi Avatar asked May 19 '11 10:05

Loupi


2 Answers

With C#.NET you can subtract one DateTime from another, resulting in a TimeSpan. For example:

TimeSpan timespan = (DateTime.Now - new DateTime(2011, 1, 1)); 

If you want a date in between two dates, you can then add half of this timespan to one of the dates:

TimeSpan timespan = (DateTime.Now - new DateTime(2011, 1, 1)); DateTime inBetween = DateTime.Now.AddDays(timespan.TotalDays / 2); 
like image 159
ColinE Avatar answered Sep 23 '22 23:09

ColinE


TimeSpan ts = Convert.ToDateTime(dr["datereturned"])              - Convert.ToDateTime(dr["dateborrowed"]);  (ts.TotalDays); // Will return the difference in Days 
like image 29
Muhammad Akhtar Avatar answered Sep 26 '22 23:09

Muhammad Akhtar