50

How can I find the start-date and name (1, 2, 3, etc.) of a quarter from a given date?

cllpse
  • 21,396
  • 37
  • 131
  • 170

10 Answers10

130

Something like (untested):

DateTime date;
int quarterNumber = (date.Month-1)/3+1;
DateTime firstDayOfQuarter = new DateTime(date.Year, (quarterNumber-1)*3+1,1);
DateTime lastDayOfQuarter = firstDayOfQuarter.AddMonths(3).AddDays(-1);
Joe
  • 122,218
  • 32
  • 205
  • 338
  • 2
    @roosteronacid: `(quarterNumber-1)*3+1` will give the month number of the first month of the given quarter. The code creates a `DateTime` for the first day of that month of the year. That is the first day of the quarter. Then it adds three months. That will be the first day of the *next* quarter, so the last day of the wanted quarter will be the day before that (`.AddDays(-1)` does that trick). – Fredrik Mörk Sep 29 '09 at 12:53
  • See @il_guru answer here => http://stackoverflow.com/questions/11154673/get-the-correct-week-number-of-a-given-date – mynkow Jan 06 '14 at 13:30
  • @Joe, is there a reason why you're doing `3+1` instead of just `4`? – Simon Sep 09 '14 at 09:28
  • 3
    @simonlchilds - yes, it's multiplying by three then adding one, not multiplying by four. I.e. equivalent to `((date.Month-1)/3) + 1` and `((quarter-1)*3) + 1` – Joe Sep 09 '14 at 11:35
  • 1
    There's an error in the code. If it's _01.01.2017_ you will get wrong year of the first day of the quarter. It will be _01.10.2017_. The right way to get first day of `var firstDayOfQuarter = new DateTime(date.AddMonths(-3).Year, (quarterNumber-1)*3+1,1);` – Dmitrii Polianskii Dec 29 '16 at 11:47
  • @DmitriyPolyanskiy - I think you're mistaken, try this: https://dotnetfiddle.net/we5IKq – Joe Dec 29 '16 at 20:06
  • @DmitriyPolyanskiy Joe calculation is the correct form, I have used code-base derivatives of this standard form. Make sure you follow (1) the Constructor parameter order and (2) the Parenthesis....Although I would have wrapped each of the calculation blocks instead of relying on PEMDAS application rules – GoldBishop Mar 03 '17 at 14:26
7
int GetQuarterName(DateTime myDate)
{
    return (int)Math.Ceiling(myDate.Month / 3.0);
}

DateTime GetQuarterStartingDate(DateTime myDate)
{
    return new DateTime(myDate.Year,(3*GetQuarterName(myDate))-2,1);
}

GetQuarterName gets the "next" integer value of the current month number / 3.

GetQuarterStartingDate uses the output from GetQuarterName to work out the month value, the year part of the original date, and 1 to represent the first day of the month to return.

(Apologies for making no sense, I have flu. :( )

Manuel
  • 10,153
  • 5
  • 41
  • 60
ZombieSheep
  • 29,603
  • 12
  • 67
  • 114
4

https://msdn.microsoft.com/en-us/library/ms127415(v=vs.110).aspx

using Microsoft.VisualBasic;
var quarter = DateAndTime.DatePart(DateInterval.Quarter, (DateTime)dateTimePickerDateTime.Value);
shriek
  • 5,157
  • 2
  • 36
  • 42
anonymous
  • 41
  • 1
2
        var date = new DateTime(2015, 3, 15);

        var quarter = (date.Month + 2) / 3;

        var quarterStartMonth = 3 * quarter - 2;

        var quarterStartDate = new DateTime(date.Year, quarterStartMonth, 1);
youzer
  • 493
  • 4
  • 11
2

I think this solution would work pretty well. It takes up more lines, but is very verbose! `

private DateTime GetFirstDayOfYearlyQuarter(DateTime value)
{
    switch (value.Month)
    {
        case 1:
        case 2:
        case 3:
            return new DateTime(value.Year, 1, 1);
        case 4:
        case 5:
        case 6:
            return new DateTime(value.Year, 4, 1);
        case 7:
        case 8:
        case 9:
            return new DateTime(value.Year, 7, 1);
        case 10:
        case 11:
        case 12:
            return new DateTime(value.Year, 10, 1);
        default:
            throw new Exception(@"¯\_(ツ)_/¯");
    }
}

`

P.S. This only finds first day of quarter, but you can easily extend this to find number of quarter, etc.

Ignas
  • 389
  • 2
  • 13
1
DateTime date;
int quarterNumber = (date.Month-1)/3+1;
DateTime firstDayOfQuarter = new DateTime(date.Year, (quarterNumber-1)*3+1,1);
DateTime lastDayOfQuarter = firstDayOfQuarter.AddMonths(3).AddDays(-1);
  • 2
    Welcome to Stack Overflow. Code dumps without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please [edit] your question and explain how it works better than what the OP provided. See [answer]. – ChrisGPT was on strike Nov 20 '20 at 13:16
0

A simpler two liner with live demo here + press F8 to run .

var date = DateTime.Now; //Give you own DateTime
int offset = 2, monthsInQtr = 3;

var quarter = (date.Month + offset) / monthsInQtr; //To find which quarter 
var totalMonths = quarter * monthsInQtr;

var startDateInQtr = new DateTime(date.Year, totalMonths - offset, 1); //start date in quarter 

If you are looking at last day of the quarter use DateTime.DaysInMonth

var endDateInQtr = new DateTime(date.Year, totalMonths, DateTime.DaysInMonth(date.Year, totalMonths));
super cool
  • 6,003
  • 2
  • 30
  • 63
0
    public static class DateTimeExtensions
{
    public static DateTime StartOfQuarter(this DateTime _date)
    {
        var quarter = decimal.ToInt16(Math.Ceiling(_date.Month / 3.0m));
        return new DateTime(_date.Year, quarter, 1);
    }
}

use

var quarterStart = DateTime.Today.StartOfQuarter();
Vernard Sloggett
  • 336
  • 4
  • 10
0

Let's begin with the start date of the quarter. I consider this one more useful, and harder to find a neat implementation for.

  • Year: The input year.
  • Month: The input month, minus its "overshoot" over the quarter's first month (e.g. February overshoots the quarter's starting month (January) by 1).
  • Day: 1
public static DateOnly GetFirstDayOfCurrentQuarter(DateTime dateTime)
{
    var month = dateTime.Month - (dateTime.Month - 1) % 3;
    var result = new DateOnly(dateTime.Year, month, 1);

    return result;
}

By making the current month 0-based (dateTime.Month - 1), we can easily see by how much it overshoots the start of its quarter (% 3). We subtract the overshoot from the current month to get the quarter's starting month.

The quarter number (1-4) is even simpler:

var quarterNumber = (dateTime.Month - 1) / 3 + 1;

By making the current month 0-based (dateTime.Month - 1), we can easily obtain the 0-based quarter index (/ 3). We convert that to the quarter number (+ 1).

Timo
  • 7,992
  • 4
  • 49
  • 67
0

Here i have 3 methods. A get the current quarter number, get the start date and end date.

static (DateTime startDate, DateTime endDate) GetCurrentQuarter(DateTime date)
{
    var year = date.Year;
    var quarter = (int)Math.Ceiling(date.Month / 3d);
    var start = GetStartDateOfQuarter(year, quarter);
    var end = GetEndDateOfQuarter(year, quarter);
    return (start, end);
}

static DateTime GetStartDateOfQuarter(int year, int quarter)
{
    var month = quarter * 3 - 2;
    return new DateTime(year, month, 1);
}

static DateTime GetEndDateOfQuarter(int year, int quarter)
{
    var month = quarter * 3;
    var daysInMonth = DateTime.DaysInMonth(year, month);
    return new DateTime(year, month, daysInMonth);
}

And then for testing purposes:

var date = new DateTime(2023, 1, 1); // 2023-01-01

var (start, end) = GetCurrentQuarter(date);

Console.WriteLine("Start date of Current Quarter: {0}", start);
Console.WriteLine("End date of Current Quarter: {0}", end);
Mo D Genesis
  • 5,187
  • 1
  • 21
  • 32