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 571455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:30:32+00:00 2026-05-13T13:30:32+00:00

I need to create a function to return the next processing date for a

  • 0

I need to create a function to return the next processing date for a given item. An item has a number that represents the month within a quarter that it is processed, as well as a number that represents the week within that month when it is processed. So, given a particular item’s create date, I need to get the next processing date for that item, which will be the first day of it’s assigned week and month within a quarter.

Note that weeks are broken out by 7 days from the start of the month, regardless of what day of the week. So the first day of the first week could start on Tuesday or any other day for the purposes of this calculation.

Example:
Let’s say I have an item with a completed date of 1/8/2010. That item has a monthWithinQuarter value of 2. It has a weekWithinMonth value of 3. So for this item that resolves to the third week of February, so I would want the function to return a date of 2/15/2010.

The function should look something like this:

var nextProcessingDate = GetNextProcessingDate(
                             itemCompletedDate,
                             monthWithinQuarter,
                             weekWithinMonth);

This calculation has to be pretty fast as this calculation is going to be happening a lot, both in real time to display on a web site as well as in batch mode when processing items.

Thanks,

~ Justin

  • 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-13T13:30:33+00:00Added an answer on May 13, 2026 at 1:30 pm

    Okay, this should do it for you:

    static DateTime GetNextProcessingDate(
        DateTime itemCompletedDate,
        int monthWithinQuarter,
        int weekWithinMonth
    ) {
            if (monthWithinQuarter < 1 || monthWithinQuarter > 3) {
                throw new ArgumentOutOfRangeException("monthWithinQuarter");
            }
            if (weekWithinMonth < 1 || weekWithinMonth > 5) {
                throw new ArgumentOutOfRangeException("weekWithinMonth");
            }
            int year = itemCompletedDate.Year;
            DateTime[] startOfQuarters = new[] {
                new DateTime(year, 1, 1),
                new DateTime(year, 4, 1),
                new DateTime(year, 7, 1),
                new DateTime(year, 10, 1)
            };
            DateTime startOfQuarter = startOfQuarters.Where(d => d <= itemCompletedDate)
                                                     .OrderBy(d => d)
                                                     .Last();
            int month = startOfQuarter.Month + monthWithinQuarter - 1;
            int day = (weekWithinMonth - 1) * 7 + 1;
            if (day > DateTime.DaysInMonth(year, month)) {
                throw new ArgumentOutOfRangeException("weekWithinMonth");
            }
            DateTime candidate = new DateTime(year, month, day);
            if (candidate < itemCompletedDate) {
                month += 3;
                if(month > 12) {
                    year++;
                    month -= 12;
                }
            }
            return new DateTime(year, month, day);
        }
    

    As far as efficiency, the place where I see the most room for improvement is repeatedly creating the array

    DateTime[] startOfQuarters = new[] {
        new DateTime(year, 1, 1),
        new DateTime(year, 4, 1),
        new DateTime(year, 7, 1),
        new DateTime(year, 10, 1)
    };
    

    So let’s offload that to a method and memoize it:

    static Dictionary<int, DateTime[]> cache = new Dictionary<int, DateTime[]>();
    public static DateTime[] StartOfQuarters(DateTime date) {
        int year = date.Year;
        DateTime[] startOfQuarters;
        if(!cache.TryGetValue(year, out startOfQuarters)) {
            startOfQuarters = new[] {
                new DateTime(year, 1, 1),
                new DateTime(year, 4, 1),
                new DateTime(year, 7, 1),
                new DateTime(year, 10, 1)
            };
            cache.Add(year, startOfQuarters);
        }
        return startOfQuarters;
    }
    

    If you don’t need the flexibility of quarters possibly starting on unusual days, you could replace

    DateTime[] startOfQuarters = new[] {
        new DateTime(year, 1, 1),
        new DateTime(year, 4, 1),
        new DateTime(year, 7, 1),
        new DateTime(year, 10, 1)
    };
    DateTime startOfQuarter = startOfQuarters.Where(d => d <= itemCompletedDate).OrderBy(d => d).Last();
    int month = startOfQuarter.Month + monthWithinQuarter - 1;
    

    with

    int month = 3 * ((itemCompletedDate.Month - 1) / 3) + monthWithinQuarter;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a function that takes month and year as parameters and
I need to create a javascript function that checks if it has been a
I need to create a function that uses a loop. This function will open
I need to create a function that, among other things, spawns a child process.
I need to create a function that receives a string and checks if the
i am trying to create sql procedure or function that need to find duplicate
I need to create a function which rounds decimal numbers like this: Round($32.95, 0)
i need to create a function in my application to set its available memory
I need to create a php search function for names and need to change
I need to be able to create a seperate function which creates a total

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.