Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7800711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:43:33+00:00 2026-06-02T00:43:33+00:00

If i have a time period, lets say DateFrom and DateTo and I have

  • 0

If i have a time period, lets say DateFrom and DateTo and I have a list of Dates, These dates will be the split dates. For example:

DateTime dateFrom = new DateTime(2012, 1, 1);
DateTime dateTo = new DateTime(2012, 12, 31);

List<DateTime> splitDates = new List<DateTime>
    {
        new DateTime(2012,2,1),
        new DateTime(2012,5,1),
        new DateTime(2012,7,1),
        new DateTime(2012,11,1),
    };

List<Tuple<DateTime, DateTime>> periods = SplitDatePeriod(dateFrom, dateTo, splitDates);

I want the result to be a list of periods, so for the previous example the result should be:

(01/01/2012 - 01/02/2012)
(02/02/2012 - 01/05/2012)
(02/05/2012 - 01/07/2012)
(02/07/2012 - 01/11/2012)
(02/11/2012 - 31/12/2012)

I have already wrote a method to do that:

List<Tuple<DateTime, DateTime>> SplitDatePeriod(DateTime dateFrom, DateTime dateTo, List<DateTime> splitDates)
{
    var resultDates = new List<Tuple<DateTime, DateTime>>();

    // sort split dates
    List<DateTime> _splitDates = splitDates.OrderBy(d => d.Date).ToList();

    DateTime _curDate = dateFrom.Date;
    for (int i = 0; i <= _splitDates.Count; ++i)
    {
        DateTime d = (i < _splitDates.Count) ? _splitDates[i] : dateTo;

        // skip dates out of range
        if (d.Date < dateFrom.Date || d.Date > dateTo.Date)
            continue;

        resultDates.Add(Tuple.Create(_curDate, d));

        _curDate = d.AddDays(1);
    }
    return resultDates;
}

The Question
It looks so ugly, Is there more neat and shorter way of doing this? using Linq maybe?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:43:36+00:00Added an answer on June 2, 2026 at 12:43 am

    This is one that works and takes care of some edge cases also:

    var realDates = splitDates
        .Where(d => d > dateFrom && d < dateTo)
        .Concat(new List<DateTime>() {dateFrom.AddDays(-1), dateTo})
        .Select(d => d.Date)
        .Distinct()
        .OrderBy(d => d)
        .ToList();
    
    // now we have             (start - 1) -- split1 -- split2 -- split3 -- end
    // we zip it against          split1   -- split2 -- split3 --  end
    // and produce       start,split1 -- split1+1,split2 -- split2+1,split3 -- split3+1,end
    
    realDates.Zip(realDates.Skip(1), (a, b) => Tuple.Create(a.AddDays(1), b));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example: I have 100 samples for some certain time period. But I can use
I have a class DurationFormatter as below: import java.util.Date; import org.joda.time.DateTime; import org.joda.time.Period; import
For a period of time, I have been dealing with an issue in pagination
Example, I have time zone is : Europe/London or America/Los_Angeles In Perl, I can
I have set of 'codes' Z that are valid in a certain time period.
I have a chart showing a large time period on the X-Axis (about a
I have a SQL Server database with a Date column. These dates are all
I have a need to take a time period, e.g. a week and break
Does Java have a data type that represents a period of time eg 34
I have the following table SEASONS with time periods defining a booking prices for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.