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.
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.