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

  • Home
  • SEARCH
  • 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 949607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:25:44+00:00 2026-05-15T23:25:44+00:00

Example: given a continuous list of date range List[0] = from 2001 Jan 01

  • 0

Example: given a continuous list of date range

List[0] = from 2001 Jan 01 to 2001 Aug 14

List[1] = from 2001 Aug 15 to 2002 Jul 10

Let’s assume that a financial year is from 1st of July to 30th of June (of next year) so the output should be

AnotherList[0] = from 2000 Jul 01 to 2001 Jun 30

  period: 2001 Jan 01 to 2001 Jun 30

AnotherList[1] = from 2001 July 01 to 2002 Jun 30

  period: 2001 Jul 01 to 2001 Aug 14
  period: 2001 Aug 15 to 2002 Jun 30

AnotherList[2] = from 2002 July 01 to 2003 Jun 30

  period: 2002 Jul 01 to 2002 Jul 10

Again it’s very easy to work out by hand but my method contains close to 100 lines of code with the combination of if else, for each and while loops which I think it’s ugly. I am trying to simplify the algorithm so that it’s easier to maintain and debug. Thanks in advance.

  • 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-05-15T23:25:44+00:00Added an answer on May 15, 2026 at 11:25 pm

    EDIT: Changed to include clearer requirements.

    Given a list that contains contiguous date ranges, the code doesn’t have to be hard at all. In fact, you don’t even have to write an actual loop:

    public const int FYBeginMonth = 7, FYBeginDay = 1;
    
    public static int FiscalYearFromDate(DateTime date)
    {
        return date.Month > FYBeginMonth ||
               date.Month == FYBeginMonth && date.Day >= FYBeginDay ?
            date.Year : date.Year - 1;
    }
    
    public static IEnumerable<DateRangeWithPeriods>
                  FiscalYears(IEnumerable<DateRange> continuousDates)
    {
        int startYear = FiscalYearFromDate(continuousDates.First().Begin),
            endYear = FiscalYearFromDate(continuousDates.Last().End);
        return from year in Enumerable.Range(startYear, endYear - startYear + 1)
               select new DateRangeWithPeriods {
                   Range = new DateRange { Begin = FiscalYearBegin(year),
                                           End = FiscalYearEnd(year) },
          // start with the periods that began the previous FY and end in this FY
                   Periods = (from range in continuousDates
                              where FiscalYearFromDate(range.Begin) < year
                                 && FiscalYearFromDate(range.End) == year
                              select new DateRange { Begin = FiscalYearBegin(year),
                                                     End = range.End })
                              // add the periods that begin this FY
                      .Concat(from range in continuousDates
                              where FiscalYearFromDate(range.Begin) == year
                              select new DateRange { Begin = range.Begin,
                                     End = Min(range.End, FiscalYearEnd(year)) })
                              // add the periods that completely span this FY
                      .Concat(from range in continuousDates
                              where FiscalYearFromDate(range.Begin) < year
                                 && FiscalYearFromDate(range.End) > year
                              select new DateRange { Begin = FiscalYearBegin(year),
                                                     End = FiscalYearEnd(year) })
    
               };
    }
    

    This assumes some DateRange structures and helper functions, like this:

    public struct DateRange
    {
        public DateTime Begin { get; set; }
        public DateTime End { get; set; }
    }
    
    public class DateRangeWithPeriods
    {
        public DateRange Range { get; set; }
        public IEnumerable<DateRange> Periods { get; set; }
    }
    private static DateTime Min(DateTime a, DateTime b)
    {
        return a < b ? a : b;
    }
    
    public static DateTime FiscalYearBegin(int year)
    {
        return new DateTime(year, FYBeginMonth, FYBeginDay);
    }
    
    public static DateTime FiscalYearEnd(int year)
    {
        return new DateTime(year + 1, FYBeginMonth, FYBeginDay).AddDays(-1);
    }
    

    This test code:

    static void Main()
    {
        foreach (var x in FiscalYears(new DateRange[] { 
            new DateRange { Begin = new DateTime(2001, 1, 1),
                            End = new DateTime(2001, 8, 14) },
            new DateRange { Begin = new DateTime(2001, 8, 15),
                            End = new DateTime(2002, 7, 10) } }))
        {
            Console.WriteLine("from {0:yyyy MMM dd} to {1:yyyy MMM dd}",
                              x.Range.Begin, x.Range.End);
            foreach (var p in x.Periods)
                Console.WriteLine(
                "    period: {0:yyyy MMM dd} to {1:yyyy MMM dd}", p.Begin, p.End);
        }
    }
    

    outputs:

    from 2000 Jul 01 to 2001 Jun 30
        period: 2001 Jan 01 to 2001 Jun 30
    from 2001 Jul 01 to 2002 Jun 30
        period: 2001 Jul 01 to 2001 Aug 14
        period: 2001 Aug 15 to 2002 Jun 30
    from 2002 Jul 01 to 2003 Jun 30
        period: 2002 Jul 01 to 2002 Jul 10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 466k
  • Answers 466k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to start the server. You should be able… May 16, 2026 at 1:44 am
  • Editorial Team
    Editorial Team added an answer Use HTTP's basic authentication (shown in the URL I've quoted… May 16, 2026 at 1:44 am
  • Editorial Team
    Editorial Team added an answer Not the prettiest solution I'm sure, but one thing we've… May 16, 2026 at 1:44 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.