Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate the start-date and name of a quarter from a given date

Tags:

date

c#

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

like image 464
cllpse Avatar asked Sep 29 '09 11:09

cllpse


People also ask

How do you calculate a quarter from a date?

To get quarter from a date, you simply need to divide the month by 3 and round up the result to the nearest integer.

How do you find the year and quarter from a date?

="Q" &INT((MONTH(A2)+2)/3) We can type this formula into cell B2 and drag the formula down to every remaining cell in column B: The quarter for each date in column A is shown in column B.


6 Answers

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);
like image 149
Joe Avatar answered Oct 06 '22 04:10

Joe


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. :( )

like image 43
ZombieSheep Avatar answered Oct 06 '22 02:10

ZombieSheep


        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);
like image 44
youzer Avatar answered Oct 06 '22 04:10

youzer


I think this solution would work pretty well. It takes up more line, bus 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.

like image 34
Ignas Avatar answered Oct 06 '22 02:10

Ignas


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

using Microsoft.VisualBasic;
var quarter = DateAndTime.DatePart(DateInterval.Quarter, (DateTime)dateTimePickerDateTime.Value);
like image 23
anonymous Avatar answered Oct 06 '22 04:10

anonymous


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);
like image 31
user14675596 Avatar answered Oct 06 '22 02:10

user14675596