tags:

views:

57

answers:

1

I am doing an activity monitor based on date which is similar to stackoverflow Today,YesterDay,this week,Last week,this month,last Month..... Based on current date how to get start-date and end-date for all these Today,YesterDay,this week,Last week,this month,last Month in c#?

+4  A: 
DateTime baseDate = DateTime.Today;

var today = baseDate;
var yesterday = baseDate.AddDays(-1);
var thisWeekStart = baseDate.AddDays(-(int)baseDate.DayOfWeek);
var thisWeekEnd = thisWeekStart.AddDays(7).AddSeconds(-1);
var lastWeekStart = thisWeekStart.AddDays(-7);
var lastWeekEnd = thisWeekStart.AddSeconds(-1);
var thisMonthStart = baseDate.AddDays(1 - baseDate.Day);
var thisMonthEnd = thisMonthStart.AddMonths(1).AddSeconds(-1);
var lastMonthStart = thisMonthStart.AddMonths(-1);
var lastMonthEnd = thisMonthStart.AddSeconds(-1);
Adam Robinson
Man, I hate when people catch their own mistakes that quickly. :)
MusiGenesis
@MusiGenesis: Haha, sorry about that! ;)
Adam Robinson
@Adam worked pretty well...
chandru_cp
@Adam plz help me out http://stackoverflow.com/questions/2821358/what-is-wrong-with-this-sql-statement
chandru_cp