3

I need to calculate a StartDate and EndDate of a current Quarter and the previous Quarter in vb.net.

Slee
  • 27,498
  • 52
  • 145
  • 243
  • business quarters, so the first quarter of would be Jan/Feb/March – Slee Feb 20 '09 at 02:41
  • Okay, that makes solving the problem fairly straightforward; I'll post an answer below. The point of my question is that each company and each country use their own definition of "quarter." – jason Feb 20 '09 at 02:52

4 Answers4

11

I know you specified VB.Net, but I'm far more comfortable writing uncomplied code in C#. I'm sure someone can translate if necessary.

public static DateTime QuarterEnd() {
    DateTime now = DateTime.Now;
    int year = now.Year;
    DateTime[] endOfQuarters = new DateTime[] {
        new DateTime(year, 3, 31),
        new DateTime(year, 6, 30),
        new DateTime(year, 9, 30),
        new DateTime(year, 12, 31)
    };
    return endOfQuarters.Where(d => d.Subtract(now).Days >= 0).First();
}

I prefer this solution over others that involve modulo arithmetic and ugly conditionals in that it is fairly easily modified to handle non-standard quarter definitions. The definition of a QuarterStart method is handled similarly, and is left as an exercise for the dear reader . Alternatively, you can modify this method to return a struct containing the desired starting and ending dates.

Edit, added VB.Net-version of above C#-code /Stefan:

Public Function QuarterEnd() As Date
    Dim endOfQuarters As Date() = New Date() { _
        New Date(Now.Year, 3, 31), _
        New Date(Now.Year, 6, 30), _
        New Date(Now.Year, 9, 30), _
        New Date(Now.Year, 12, 31)}
    Return endOfQuarters.Where(Function(d) d.Subtract(Now).Days >= 0).First()
End Function
Stefan
  • 11,423
  • 8
  • 50
  • 75
jason
  • 236,483
  • 35
  • 423
  • 525
3

Forgive my lack of a VB.net-specific answer, but given a generic sort of date object with various functions in some pseudo-language (with zero-based day and month indexes)...

//round the current month number down to a multiple of 3
ThisQuarterStart = DateFromYearMonthDay(Today.Year,Today.Month-(Today.Month%3),0);
//round the current month number up to a multiple of 3, then subtract 1 day
ThisQuarterEnd = DateFromYearMonthDay((Today.Month<9)?(Today.Year):(Today.Year+1),(Today.Month-(Today.Month%3)+3)%12,0) - 1;
//same as above, but minus 3 months
LastQuarterStart = DateFromYearMonthDay((Today.Month<3)?(Today.Year-1):(Today.Year),(Today.Month-(Today.Month%3)+9)%12,0)
LastQuarterEnd = ThisQuarterStart - 1;

Edit converted above pseudocode to working VB.Net: /Stefan

Dim ThisQuarterStart As Date = New Date(Today.Year, Today.Month - (Today.Month Mod 3) + 1, 1)  
Dim ThisQuarterEnd As Date = New Date(CInt(IIf(Today.Month < 9, Today.Year, Today.Year + 1)), (Today.Month - (Today.Month Mod 3) + 3 Mod 12) + 1, 1).AddDays(-1)
Dim LastQuarterStart As Date = New Date(CInt(IIf(Today.Month < 3, Today.Year - 1, Today.Year)), (Today.Month - (Today.Month Mod 3) + 9 Mod 12) + 1, 1)
Dim LastQuarterEnd As Date = ThisQuarterStart.AddDays(-1)
Stefan
  • 11,423
  • 8
  • 50
  • 75
Sparr
  • 7,489
  • 31
  • 48
  • sweet! I am so tired and burnt right now this was a real help. – Slee Feb 20 '09 at 03:41
  • 1
    This does not work, if today is December. It gives "Year, Month, and Day parameters describe an un-representable DateTime." error. – Oleksandr Dec 02 '15 at 07:20
2

The vb.net convertion needs a change. Changed it to vb 6 and it should read;

ThisQuarterStart = DateSerial(today.Year, today.Month - (IIf(today.Month Mod 3 = 0, 3, today.Month Mod 3)) + 1, 1)

ThisQuarterEnd = DateSerial(CInt(IIf(today.Month < 9, today.Year, today.Year + 1)), ((today.Month - (IIf(today.Month Mod 3 = 0, 3, today.Month Mod 3)) + 3) Mod 12) + 1, 1) - 1

0

Here is a simple way to obtain the start date of the quarter for any given DateTime in C#. Converting the code VB.NET should be trivial.

From there, it's mostly easy:

var startOfCurrentQuarter = GetFirstDayOfCurrentQuarter(dateTime);
var startOfNextQuarter = startOfCurrentQuarter.AddMonths(3);
var endOfCurrentQuarter = startOfNextQuarter.AddDays(-1);
var endOfNextQuarter = startOfNextQuarter.AddMonths(3).AddDays(-1);

Beware of the last one. It's tempting to try to get the end of next quarter by doing endOfCurrentQuarter.AddMonths(3), but that is likely to land on the wrong day (September 30 => December 30).

Timo
  • 7,992
  • 4
  • 49
  • 67