I’m trying to get the number of days (calculated byu datediff) in sql and the number of days in c# (calculated by DateTime.now.Substract) to be the same, but they return different results….
//returns 0
int reso = DateTime.Now.Subtract(expirationDate).Days;
vs
//returns 1
dateDiff(dd,getDate(),ExpirationDate)
In both cases, ExpirationDate is ’10/1/2011 00:00:00′, and the code and the DB are sitting on the same server. I want the return int to be the same. I suspect I’m missing something stupid… ideas??
10/1/2011 is less than 1 day away from DateTime.Now. Since you’re getting back a TimeSpan and then applying Days to it, you’re getting back a TimeSpan that is < 1 day. So it’ll return 0 Days.
Instead, just use the Date component of those DateTimes and it’ll correctly report the number of days apart – like this:
This will yield you 1 day.