How do I discover the quarter of a given date

If you prefer short and concise solutions without branching and arrays, here is my preferred solution.

Normal Quarter:

public static int GetQuarter(this DateTime date)
{
    return (date.Month + 2)/3;
}

Financial Year Quarter:

public static int GetFinancialQuarter(this DateTime date)
{
    return (date.AddMonths(-3).Month + 2)/3;
}

Integer division will truncate decimals, giving you an integer result. Place methods into a static class and you will have an extension method to be used as follows:

date.GetQuarter()
date.GetFinancialQuarter()

See dotnetfiddle

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)