I’m trying to find all quarters between 2 given dates.
For example if the current date is today(15 Jan 2013) and a date in the past is 01 Jan 2012, I should be getting the following result:
Dec 2012
Sep 2012
June 2012
March 2012
I can kind of keep deducting 3 months from the current date, but that is not going to be accurate as the test below denotes:
[TestMethod]
public void FindAllQuaters_FromAGivenDate_TillAPastDate()
{
var dateFinder = new FindQuarter();
//Between Feb 2013 & Jan 2012
var dates = dateFinder.GetAllQuarters(new DateTime(2013, 02, 01), new DateTime(2012, 01, 01)).ToList();
Assert.IsTrue(dates.Count == 4);
Assert.IsTrue(dates.First().Month == 12);
Assert.IsTrue(dates.First().Year == 2012);
Assert.IsTrue(dates.Skip(1).First().Month == 9);
Assert.IsTrue(dates.Skip(1).First().Year == 2012);
Assert.IsTrue(dates.Skip(2).First().Month == 6);
Assert.IsTrue(dates.Skip(2).First().Year == 2012);
Assert.IsTrue(dates.Skip(3).First().Month == 3);
Assert.IsTrue(dates.Skip(3).First().Year == 2012);
}
}
public class FindQuarter
{
public IEnumerable<DateTime> GetAllQuarters(DateTime current, DateTime past)
{
while (current > past)
{
current = current.AddMonths(-3);
yield return current;
}
}
}
How can I achieve this? The quarters are defined as March,June,September & December. I would also need the last date of that month(which I hope would be quick).
EDIT: This is not working because it keeps deducting 3 months from a given date, for example if the current date is feb 2013 and I reduce 3 months,I get Nov 2012 and not December 2012.
-
Any dates in Jan,Feb,March will belong to first quarter i.e March
-
Any dates in April,May,June will belong to second quarter i.e June
-
Any dates in July,Aug,Sep will belong to third quarter i.e September
-
Any dates in Oct,Nov,Dec will belong to fourth quarter i.e December
This seems to work.