views:

2110

answers:

4

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

+4  A: 

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)
Sparr
I like onelinersolutions!
Stefan
And the fact that its working.;)
Stefan
sweet! I am so tired and burnt right now this was a real help.
Slee
+8  A: 

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
Jason
+1 for the simplicity of it.
Stefan
@Stefan: Thanks for adding the VB.Net version.
Jason
+1, I like the implementation.
Sparr
+1  A: 

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