Slee
Slee

Reputation: 28248

Calculate StartDate and EndDate for This Quarter and Last Quarter

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

Upvotes: 3

Views: 10445

Answers (4)

Timo
Timo

Reputation: 8680

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

Upvotes: 0

samrat
samrat

Reputation:

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

Upvotes: 2

jason
jason

Reputation: 241585

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

Upvotes: 11

Sparr
Sparr

Reputation: 7712

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)

Upvotes: 3

Related Questions