Should I make a DateRange object?

No, you didn’t miss a general purpose class.

I have a Range type in MiscUtil which you may be interested in – and it certainly makes for simple DateTime manipulation. Referring to Marc’s answer, I can’t remember whether this is a struct or a class – you’d be welcome to change it of course.

It’s nice and easy to step through, due to Marc’s generics shenanigans (assuming you’re using .NET 3.5, at least – it’s feasible with 2.0 but not supported at the moment);

Range<DateTime> range = 19.June(1976).To(DateTime.Today);

foreach (DateTime date in range.Step(1.Days())
{
    // I was alive in this day
}

(That’s also using a bunch of extension methods – more useful for test than production.)

To address the other point in Marc’s answer, Noda Time will certainly be able to express the concept of a date more appropriately than the .NET API, but we don’t have anything like a range at the moment… It’s a nice idea though – I’ve added a feature request.

Leave a Comment