You could write a function to return the current quarter, and then just subtract 3 months from the date repeatedly to get the previous quarters:
public static int GetQuarter(DateTime date)
{
int[] quarters = new int[] { 4,4,4,1,1,1,2,2,2,3,3,3 };
return quarters[date.Month-1];
}
** Note, of course update this code to reflect which quarters your months apply to. Function from (http://stackoverflow.com/questions/8698303/how-do-i-discover-the-quarter-of-a-given-date-in-c-net-3-5-or-4-0https://stackoverflow.com/questions/8698303/how-do-i-discover-the-quarter-of-a-given-date-in-c-net-3-5-or-4-0)
But then it becomes trivial to get the current and previous quarters:
DateTime date = DateTime.Now;
var currentQuarter = GetQuarter(date);
date = date.AddMonths(-3);
var prevQuarter1 = GetQuarter(date);
date = date.AddMonths(-3);
var prevQuarter2 = GetQuarter(date);
date = date.AddMonths(-3);
var prevQuarter3 = GetQuarter(date);
Hopefully this is a good solution to cleaning up your code some. You can test it out here on csharppad: http://csharppad.com/gist/c0bea5365ec515a077db
Since you have access to the date object, you will have the year already too. If you need both the quarter and the year as part of the GetQuarter function, you can return a custom object that stores both values, i.e.:
public class Quarter
{
public int Quarter { get; set; }
public int Year { get; set;}
}
public static Quarter GetQuarter(DateTime date)
{
int[] quarters = new int[] { 4,4,4,1,1,1,2,2,2,3,3,3 };
Quarter quarter = new Quarter();
quarter.Quarter = quarters[date.Month-1];
quarter.Year = date.Year;
return quarter;
}