You are better off creating a DateRange
value type than using DateTime
and TimeSpan
. Look here for an example. Then you can use the factory method, which gives you a range for a decade: DateRange.DecadeStartingOn(1930);
. Thus, you raise the level of abstraction and understand the concepts that you have in mind in the code itself.
Your IsInsidePeriod
is a simple operation for DateRange
:
public bool Includes(DateTime date) { return start <= date && date <= end; }
(assuming both start
and end
are included)
Now, if you only need to deal with decades, you really don't need a full-fledged DateRange
class, just this:
class Decade { public int StartYear { get; private set; } public int EndYear { get { return StartYear + 9; } } public Decade(int startYear) { StartYear = startYear; } public bool Includes(DateTime date) { return StartYear <= date.Year && date.Year <= EndYear; } public override string ToString() { return string.Format("{0}-{1}", StartYear, EndYear + 1); } }
Or maybe a more general YearRange
.
source share