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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:58:56+00:00 2026-06-14T17:58:56+00:00

Tricky one to explain this but could be an interesting challenge to find the

  • 0

Tricky one to explain this but could be an interesting challenge to find the simplest method, I spent several hours yesterday trying to refactor a .NET C# bit of code but did not come up with anything elegant.

So anyone with a good JavaScript, Math, Date problem solving head might have a good light bulb moment!!

I basically need to be able to generate the start (and end) for a date range. i.e. Last 12 months, Last 6 months, Last 24 months etc.

BUT it’s not just a simple CurrentDate subtract year type problem.

We use period ranges to visualise the data we are presenting, i.e. monthly, bimonthly, quarterly, half yearly. But we also have a reference month where the period ranges should be based from, i.e. the period range should always start on July. So you always get a clean cut quarter starting from 1 July and going to 30 Sept, no matter what the current date etc.

With that in mind we always want the start date to take this reference into account, the reference will always just be a month so all start dates will be the 1st of a month. The end date will be the last day of a month and FYI will most of the time actually be after the current date, because we want the final period range to include today’s date.

So we need a function which you can pass in (I have included sample values):

currentDate = 21 NOV 2012
dateRangeInMonths = 12 (Year)
periodRangeInMonths = 3 (Quarterly)
periodRangeReferenceStartMonth = 7 (July)

The answer for the above would be the following because it would fit the following ranges:

01 JAN 2012 - 31 MAR 2012
01 APR 2012 - 31 JUN 2012
01 JUL 2012 - 30 SEP 2012
01 OCT 2012 - 31 DEC 2012

returns startDate = 01 JAN 2012
returns endDate = 31 DEC 2012

So this is correct because we have the period starts in line with the reference month of July, we are including today’s date in the final range.

It also needs to be good for overlapping into previous years obviously. So another example could be:

currentDate = 10 FEB 2012
dateRangeInMonths = 6 (Half Year)
periodRangeInMonths = 2 (Bimonthly)
periodRangeReferenceStartMonth = 1 (Jan)

Answer:

01 SEP 2011 - 31 OCT 2011
01 NOV 2011 - 31 DEC 2011
01 JAN 2012 - 29 FEB 2012

returns startDate = 01 SEP 2011
returns endDate = 29 FEB 2012

Also if its of any help this is a c# bit of code that does what I want but is far from elegant, and relies on looping back in time etc. So you might want to just ignore it haha.

if (this.PeriodRangeInMonths > 1)
{
    this.StartDate = new DateTime(DateTime.Now.Year, this.PeriodRangeReferenceStartMonth, 1).AddMonths(0 - this.DateRangeInMonths);
    var temp = DateTime.Now.AddMonths(0 - this.DateRangeInMonths).AddMonths(this.PeriodRangeInMonths);
    while (this.StartDate.AddMonths(this.PeriodRangeInMonths) < temp)
        this.StartDate = this.StartDate.AddMonths(this.PeriodRangeInMonths);
}
else this.StartDate = new DateTime(DateTime.Now.AddMonths(1).Year, DateTime.Now.AddMonths(1).Month, 1).AddMonths(0 - this.DateRangeInMonths);
this.EndDate = this.StartDate.AddMonths(this.DateRangeInMonths);

I’m also open to using any 3rd party jquery libraries that might help, with math or date functions etc.

  • 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-14T17:58:57+00:00Added an answer on June 14, 2026 at 5:58 pm

    I think I’ve got the maths down: http://jsfiddle.net/GNn7y/1/

    Here’s a javascript solution, but this shouldn’t be too hard to adapt to another language.

    function getDateRange(refDate, monthRange, periodMonths, startMonth) {
        var datePeriodOffset = Math.floor((refDate.getMonth() - startMonth)/periodMonths);
        var endMonth = startMonth + datePeriodOffset*periodMonths - 1 + periodMonths;   
        var endDate = new Date(refDate.getFullYear(), endMonth+1, 0);  // the 0 sets it to the last day of the previous month (endMonth)
        var startDate = new Date(endDate.getFullYear(), endDate.getMonth() - monthRange + 1, 1);
    
        return {
            start : startDate,
            end : endDate
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Tricky one to explain this. I have a custom built properties grid. The left
this is kinda tricky to explain,but here goes....I have multiple hidden divs that swap
Ugh, how do I explain this one... Probably a simple question but my mind
This is a tricky one to explain. So I'll bullet point it: I have
This is a tricky one to explain (and very weird), so bear with me.
This one is a little tricky to explain. I would like to create a
This is a tricky one. I am emulating ZF Bootstrapping (surface appearance). Don't ask
This one is a little tricky. Say I have this XmlDocument <Object> <Property1>1</Property1> <Property2>2</Property2>
This one has proven to be a little tricky for me so far. I
So, this one seems to be a little tricky. There's an image map with

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.