user315648
user315648

Reputation: 1965

How to group a period of time into yearly periods ? (split timespan into yearly periods)

I have a range of two datetimes:

DateTime start = new DateTime(2012,4,1);
DateTime end = new DateTime(2016,7,1);

And I wish to get all periods GROUPED BY YEAR between this period. Meaning the output has to be:

2012-04-01 - 2012-12-31
2013-01-01 - 2013-12-31
2014-01-01 - 2014-12-31
2015-01-01 - 2015-12-31
2016-01-01 - 2016-07-01

Preferably the output would be in IList<Tuple<DateTime,DateTime>> list.

How would you do this ? Is there anyway to do this with LINQ somehow ?

Oh and daylight saving time is not absolutely critical, but surely a bonus.

Thanks!

Upvotes: 2

Views: 887

Answers (4)

Omar
Omar

Reputation: 16623

What about:

static public List<Tuple<DateTime, DateTime>> GroupByYear(DateTime start, DateTime end)
{
    List<Tuple<DateTime, DateTime>> datetimes = Enumerable.Range(start.Year + 1, end.Year - 1 - start.Year)
                                                          .Select(x => new Tuple<DateTime, DateTime>(new DateTime(x, 1, 1), new DateTime(x, 12, 31)))
                                                          .ToList();
    datetimes.Add(new Tuple<DateTime, DateTime>(start, new DateTime(start.Year, 12, 31)));
    datetimes.Add(new Tuple<DateTime, DateTime>(new DateTime(end.Year, 1, 1), end));

    return datetimes.OrderBy(x => x.Item1.Year).ToList();
}

Upvotes: 1

Bob Vale
Bob Vale

Reputation: 18474

DateTime start = new DateTime(2012,4,1); 
DateTime end = new DateTime(2016,7,1); 

var dates = from year in Enumerable.Range(start.Year,end.Year-start.Year+1)
        let yearStart=new DateTime(year,1,1)
        let yearEnd=new DateTime(year,12,31)
        select Tuple.Create(start>yearStart ? start : yearStart, end<yearEnd ? end : yearEnd); 

IList<Tuple<DateTime,DateTime>> result = dates.ToList();

Upvotes: 3

Ohad Schneider
Ohad Schneider

Reputation: 38122

I didn't check all edge cases but this should mostly do it

IList<Tuple<DateTime, DateTime>> GetYearPeriods(DateTime start, DateTime end)
{   
    var years = end.Year - start.Year - 1;
    var startDates = Enumerable.Range(start.Year+1, years)
                               .Select(year => new DateTime(year, 1, 1));
    var endDates = Enumerable.Range(start.Year+1, years)
                             .Select(year => new DateTime(year, 12, 31));

    var firstPeriod = Tuple.Create(start, new DateTime(start.Year, 12, 31));
    var middlePeriods = startDates.Zip(endDates, (s,e) => Tuple.Create(s, e));
    var lastPeriod = Tuple.Create(new DateTime(end.Year, 1, 1), end);

    return 
      (new[] {firstPeriod}).Union(middlePeriods).Union(new[] {lastPeriod}).ToList();
}

Upvotes: 0

itsme86
itsme86

Reputation: 19496

    static IList<Tuple<DateTime, DateTime>> GetDateRangesByYear(DateTime start, DateTime end)
    {
        List<Tuple<DateTime, DateTime>> ranges = new List<Tuple<DateTime,DateTime>>();

        for (int year = start.Year; year <= end.Year; ++year)
        {
            DateTime yearBegin = year == start.Year ? start : new DateTime(year, 1, 1);
            DateTime yearEnd = year == end.Year ? end : new DateTime(year, 12, 31);

            ranges.Add(Tuple.Create(yearBegin, yearEnd));
        }

        return ranges;
    }

Upvotes: 3

Related Questions