sql server - Day Date difference between two date calculation in SQL AND C# producing varying result -
i calculating day difference in 2 dates. in c#
diffdays = (enddate-startdate).days
so considering enddate 6/26/2015 , startdate 6/10/2015 diffdays value 15 shown in autos section while debugging.
while in sql server doing is
select datediff(day, startdate, enddate )
where enddate 6/26/2015 , startdate 6/10/2015 , gives result 16.
i need these 2 day difference same. doing wrong?
the timespan.days
property returns whole days only, dropping fractional portion. depending on time portion of 2 datetime
's, expect behavior you're seeing.
try taking time portion out of equation using date
property (and setting both times midnight):
diffdays = (enddate.date - startdate.date).days
alternatively, can round totaldays
property (which includes fractional portions of days):
diffdays = math.ceiling((enddate - startdate).totaldays);
Comments
Post a Comment