Get Current QuarterEnd for a given FYE Date
Posted
by Rohit Gupta
on Geeks with Blogs
See other posts from Geeks with Blogs
or by Rohit Gupta
Published on Fri, 12 Mar 2010 02:06:12 GMT
Indexed on
2010/03/12
2:17 UTC
Read the original article
Hit count: 509
Here is the code to get the Current Quarter End for a Given FYE Date:
1: public static DateTime ThisQuarterEnd(this DateTime date, DateTime fyeDate)
2: {
3: IEnumerable<DateTime> candidates =
4: QuartersInYear(date.Year, fyeDate.Month).Union(QuartersInYear(date.Year + 1, fyeDate.Month));
5: return candidates.Where(d => d.Subtract(date).Days >= 0).First();
6: }
7:
8: public static IEnumerable<DateTime> QuartersInYear(int year, int q4Month)
9: {
10: int q1Month = 3, q2Month = 6, q3Month = 9;
11: int q1year = year, q2year = year, q3year = year;
12: int q1Day = 31, q2Day = 31, q3Day = 31, q4Day = 31;
13:
14:
15: q3Month = q4Month - 3;
16: if (q3Month <= 0)
17: {
18: q3Month = q3Month + 12;
19: q3year = year - 1;
20: }
21: q2Month = q4Month - 6;
22: if (q2Month <= 0)
23: {
24: q2Month = q2Month + 12;
25: q2year = year - 1;
26: }
27: q1Month = q4Month - 9;
28: if (q1Month <= 0)
29: {
30: q1Month = q1Month + 12;
31: q1year = year - 1;
32: }
33:
34: q1Day = new DateTime(q1year, q1Month, 1).AddMonths(1).AddDays(-1).Day;
35: q2Day = new DateTime(q2year, q2Month, 1).AddMonths(1).AddDays(-1).Day;
36: q3Day = new DateTime(q3year, q3Month, 1).AddMonths(1).AddDays(-1).Day;
37: q4Day = new DateTime(year, q4Month, 1).AddMonths(1).AddDays(-1).Day;
38:
39: return new List<DateTime>() {
40: new DateTime(q1year, q1Month, q1Day),
41: new DateTime(q2year, q2Month, q2Day),
42: new DateTime(q3year, q3Month, q3Day),
43: new DateTime(year, q4Month, q4Day),
44: };
45:
46: }
The code to get the NextQuarterEnd is simple, just Change the Where clause to read d.Subtract(date).Days > 0 instead of d.Subtract(date).Days >= 0
1: public static DateTime NextQuarterEnd(this DateTime date, DateTime fyeDate)
2: {
3: IEnumerable<DateTime> candidates =
4: QuartersInYear(date.Year, fyeDate.Month).Union(QuartersInYear(date.Year + 1, fyeDate.Month));
5: return candidates.Where(d => d.Subtract(date).Days > 0).First();
6: }
Also if you need to get the Quarter Label for a given Date, given a particular FYE date then following is the code to use:
1: public static string GetQuarterLabel(this DateTime date, DateTime fyeDate)
2: {
3: int q1Month = fyeDate.Month - 9, q2Month = fyeDate.Month - 6, q3Month = fyeDate.Month - 3;
4:
5: int year = date.Year, q1Year = date.Year, q2Year = date.Year, q3Year = date.Year;
6:
7: if (q1Month <= 0)
8: {
9: q1Month += 12;
10: q1Year = year + 1;
11: }
12: if (q2Month <= 0)
13: {
14: q2Month += 12;
15: q2Year = year + 1;
16: }
17: if (q3Month <= 0)
18: {
19: q3Month += 12;
20: q3Year = year + 1;
21: }
22:
23: string qtr = "";
24: if (date.Month == q1Month)
25: {
26: qtr = "Qtr1";
27: year = q1Year;
28: }
29: else if (date.Month == q2Month)
30: {
31: qtr = "Qtr2";
32: year = q2Year;
33: }
34: else if (date.Month == q3Month)
35: {
36: qtr = "Qtr3";
37: year = q3Year;
38: }
39: else if (date.Month == fyeDate.Month)
40: {
41: qtr = "Qtr4";
42: year = date.Year;
43: }
44:
45: return string.Format("{0} - {1}", qtr, year.ToString());
46: }
© Geeks with Blogs or respective owner