I searched on the internet for a quick way to get the Quarter from a DateTime, but I didn’t really find anything useful, so I created this quick extension that seems to work great! I just wanted to share this to save someone time so they don’t waste the same amount of time that I did. I am thinking about putting together an open source library to handle many other things like this, what are your thoughts about this?
public static class DateTimeExtensions
{
public static int ToQuarter(this DateTime dt)
{
int qtr = -1;
if (dt.Month >= 1 && dt.Month <= 3)
qtr = 1;
else if (dt.Month >= 4 && dt.Month <= 6)
qtr = 2;
else if (dt.Month >= 7 && dt.Month <= 9)
qtr = 3;
else if (dt.Month >= 10 && dt.Month <= 12)
qtr = 4;
return qtr;
}
}