Click here to Skip to main content
14,668,490 members
Rate this:
Please Sign up or sign in to vote.
var receiveDetails = objDB.GetAllReceivedDetails().Where(a => a.ReceiveDate.Value.Date == DateTime.Now.Date && (((DateTime.Now.Hour) - (a.ReceiveDate.Value.Hour)) <= 1));


above code is not working.If anyone have idea of making difference between two date then please reply.
Posted
Updated 18-Aug-16 21:19pm

Rate this:
Please Sign up or sign in to vote.

Solution 2

First of, never fetch DateTime.Now more than once: it leads to intermittent problem around midnight which can really mess you up. Fetch it once, and use it for all checks.
Then try:
DateTime now = DateTime.Now;
var receiveDetails = objDB.GetAllReceivedDetails().Where(a => a.ReceiveDate.Value.Date == now.Date && (now - a.ReceiveDate.Value).TotalHours <= 1);
   
Comments
Andy Lanng 3-Sep-15 11:18am
   
Yay - I won. Good point about the DateTime.Now, though. 5
OriginalGriff 3-Sep-15 11:22am
   
Faster fingers! :laugh:
Gets my five.
Rate this:
Please Sign up or sign in to vote.

Solution 1

When you subtract a datetime from a datetime you get a timespan object. You can use this to get the TotalHours which is the number you're after:

var receiveDetails = objDB.GetAllReceivedDetails()
       .Where(a => 
               a.ReceiveDate.Value.Date == DateTime.Now.Date && 
               ((DateTime.Now - a.ReceiveDate.Value).TotalHours <=1));


Hope that helps ^_^
Andy
   
v3
Comments
itsathere 3-Sep-15 11:22am
   
var receiveDetails = objDB.GetAllReceivedDetails()
.Where(a =>
a.ReceiveDate.Value.Date == DateTime.Now.Date &&
((DateTime.Now - a.ReceiveDate.Value).TotalHours <=1));

U have done mistake

btw Thanks.
Andy Lanng 3-Sep-15 11:23am
   
I think I must have fixed it before I read this comment ^_^
Rate this:
Please Sign up or sign in to vote.

Solution 3

TimeSpan difference = firstDateTime - secondDateTime;
double diffInHours = difference.TotalHours
   
Comments
phil.o 19-Aug-16 3:32am
   
Would you care not digging out old questions that already have been marked as answered, please?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100