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

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


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);

        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);

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

using Microsoft.VisualBasic;
var quarter = DateAndTime.DatePart(DateInterval.Quarter, (DateTime)dateTimePickerDateTime.Value);

Tags:

C#

Date