Click here to Skip to main content
15,860,859 members
Please Sign up or sign in to vote.
3.36/5 (3 votes)
C#
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

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:
C#
DateTime now = DateTime.Now;
var receiveDetails = objDB.GetAllReceivedDetails().Where(a => a.ReceiveDate.Value.Date == now.Date && (now - a.ReceiveDate.Value).TotalHours <= 1);
 
Share this answer
 
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.
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:

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


Hope that helps ^_^
Andy
 
Share this answer
 
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 ^_^
C#
TimeSpan difference = firstDateTime - secondDateTime;
double diffInHours = difference.TotalHours
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900