Ok I'm a moron for not seeing the tree for forest once again. Nevermind on date conversions DateTime.Now.Date gives just the date part.
Dates in .NET are always represented as DateTime values which include a time portion. Sometimes I need to use dates that are ‘just’ date values and there’s no native mechanism to extract just the date as far as I can see. The closest thing that I can see is to use the following ugly code:
DateTime DateCutOff = DateTime.Now.AddDays(days * -1);
DateCutOff = DateTime.Parse(DateCutOff.ToString("d")); // round down
Works, but really this just feels all wrong <s>.
But why is it so hard to work with dates with anything Microsoft related in the first place? SQL Server doesn’t have any notion of a date without time. For example, to run a query that retrieves a result set that is grouped by date you have to resort stuff like this:
select count(*) as Count,year(time) as yy, month(time) as mm, day(time) as dd,
ip, min(time) as Time into #TQuery
where time >= @DateCutOff
group by year(time),month(time), day(time), ip
order by 1 desc
select min(time) as Date,sum(Count) as Hits, count(*) as Visitors from #TQuery
group by yy,mm,dd order by min(Time) Desc
just to get a date sorted aggregate. How much easier would this be if there was just a simple way to round down a date?
Other Posts you might also like