I'm sure I'm not the first person to need to do this, so I'm looking for the best way.
I've got a set of radio buttons with choices such as
and I need to produce the proper relative date range from the current date (DateTime.Now
).
For example if Last Year
was selected and the current date was 4/2/09 14:45:32
I would need to return a start date of 1/1/08 00:00:00
and end date of 12/31/08 23:59:59
.
Any thoughts?
All of these have been tested using DateTime.Today, and work just like you asked for:
public struct DateRange
{
public DateTime Start { get; set; }
public DateTime End { get; set; }
}
public static DateRange ThisYear(DateTime date)
{
DateRange range = new DateRange();
range.Start = new DateTime(date.Year, 1, 1);
range.End = range.Start.AddYears(1).AddSeconds(-1);
return range;
}
public static DateRange LastYear(DateTime date)
{
DateRange range = new DateRange();
range.Start = new DateTime(date.Year - 1, 1, 1);
range.End = range.Start.AddYears(1).AddSeconds(-1);
return range;
}
public static DateRange ThisMonth(DateTime date)
{
DateRange range = new DateRange();
range.Start = new DateTime(date.Year, date.Month, 1);
range.End = range.Start.AddMonths(1).AddSeconds(-1);
return range;
}
public static DateRange LastMonth(DateTime date)
{
DateRange range = new DateRange();
range.Start = (new DateTime(date.Year, date.Month, 1)).AddMonths(-1);
range.End = range.Start.AddMonths(1).AddSeconds(-1);
return range;
}
public static DateRange ThisWeek(DateTime date)
{
DateRange range = new DateRange();
range.Start = date.Date.AddDays(-(int)date.DayOfWeek);
range.End = range.Start.AddDays(7).AddSeconds(-1);
return range;
}
public static DateRange LastWeek(DateTime date)
{
DateRange range = ThisWeek(date);
range.Start = range.Start.AddDays(-7);
range.End = range.End.AddDays(-7);
return range;
}
This year:
DateTime Today = DateTime.Today;
DateTime StartDate = new DateTime(Today.Year,1,1);
DateTime EndDate = StartDate.AddYears(1).AddSeconds(-1);
This month:
DateTime Today = DateTime.Today;
DateTime StartDate = new DateTime(Today.Year,Today.Month,1);
DateTime EndDate = StartDate.AddMonths(1).AddSeconds(-1);
This week:
DateTime Today = DateTime.Today;
DateTime StartDate = Today.AddDays(-((int) Today.DayOfWeek));
DateTime EndDate = StartDate.AddDays(7).AddSeconds(-1);
Last year / month / week are simple variations on above. Edit: This week assumes the week starts on Sunday. You would have to modify the code slightly if your weeks start on Monday.
I would create a factory method which will return an interface (or a delegate which you can execute) which would be passed the current date and return the date range for you based on the implementation.
Of course, which implementation you return from the factory method would be determined by the value of an enumeration you pass to it which corresponds to "this year", "last year", etc, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With