0
votes

I have the vb net code for first and last day of the Quarter, but I am trying to do this for first and last day of Bi monthly period ( which end on these months Feb, Apr, Jun, Aug, Oct, Dec)

So the bimonthly start and end dates are Jan 1 to Feb 28 (or 29) Mar 1 to Apr 30, May 1 to Jun 30, etc

Any ideas how to do this? (in Vb thanks)

'To get the first day of the quarter
Public Shared Function xFirstDayOfQuarter(ByVal theDay As DateTime) As DateTime
    Dim currQuarter As Integer = (Month(theDay) - 1) \ 3 + 1
    Dim FirstDayQuarter As DateTime = DateSerial(Year(theDay), 3 * currQuarter - 2, 1)
    Return FirstDayQuarter
End Function
'To get the last day of the quarter
Public Shared Function xLastDayOfQuarter(ByVal theDay As DateTime) As DateTime
    Dim currQuarter As Integer = (Month(theDay) - 1) \ 3 + 1
    Dim LastDayQuarter As DateTime = DateSerial(Year(theDay), 3 * currQuarter + 1, 0)
    Return LastDayQuarter
End Function

This how far I got ..

    Private Function DayOfBiMonth(ByVal theDay As DateTime, ByVal FirstOrLast As Integer) As DateTime
    Dim res As DateTime = Util.gBASEDATE
    Dim Mth As Integer = Month(theDay)
    Dim Yr As Integer = Year(theDay)
    Dim BiMonth As Integer = -1

    Select Case Mth
        Case Is = 1, 2
            If FirstOrLast = 1 Then
                BiMonth = 1
            ElseIf FirstOrLast = 2 Then
                BiMonth = 2
            End If
        Case Is = 3, 4
            If FirstOrLast = 1 Then
                BiMonth = 3
            ElseIf FirstOrLast = 2 Then
                BiMonth = 4
            End If
        Case Is = 5, 6
            If FirstOrLast = 1 Then
                BiMonth = 5
            ElseIf FirstOrLast = 2 Then
                BiMonth = 6
            End If
        Case Is = 7, 8
            If FirstOrLast = 1 Then
                BiMonth = 7
            ElseIf FirstOrLast = 2 Then
                BiMonth = 8
            End If
        Case Is = 9, 10
            If FirstOrLast = 1 Then
                BiMonth = 9
            ElseIf FirstOrLast = 2 Then
                BiMonth = 10
            End If
        Case Is = 11, 12
            If FirstOrLast = 1 Then
                BiMonth = 11
            ElseIf FirstOrLast = 2 Then
                BiMonth = 12
            End If
    End Select

    If FirstOrLast = 1 Then
        res = DateSerial(Yr, BiMonth, 1)
    ElseIf FirstOrLast = 2 Then
        res = DateSerial(Yr, BiMonth, 20)
        res = Util.LastDayOfMonth(res)
    End If

    Return res
End Function
Private Function LastDayOfMonth(ByVal theDay As DateTime) As DateTime
    Dim FirstDayMonth As DateTime = DateSerial(Year(theDay), Month(theDay), 1)
    Dim LastDayMonth As DateTime = FirstDayMonth.AddMonths(1).AddDays(-1)
    Return LastDayMonth
End Function
1
So what have you tried? Your code should work just by changing from 3-month groupings to 2-month groupings.Ben Voigt
Changing 3 to 2, just gives the month before, and the month after and not the Bi-Monthly Jan to Feb, Mar to Apr, May to Jun, Jul-Aug, etc only pattern.icm63
*************************icm63
What's the point of that 20 in the DateSerial call? (Not that it matters, your LastDayOfMonth throws it away anyway)Ben Voigt
The point of the 20, is just a fill, lastdayofmonth secures issues with leap yearicm63

1 Answers

0
votes

This works best

      'To get last day of BIMonth 2,4,6,8,12
    Public Shared Function LastDayOfBiMonth(ByVal theDay As DateTime) As DateTime
        Dim FirstDayBiMonth As DateTime = FirstDayOfBiMonth(theDay)
        Dim LastDayBiMonth As DateTime = FirstDayBiMonth.AddMonths(2).AddDays(-1)
        Return LastDayBiMonth
    End Function
    'To get first day of BIMonth 1,3,5,7,9,11
    Public Shared Function FirstDayOfBiMonth(ByVal theDay As DateTime) As DateTime
        Dim Mnth As Integer = Month(theDay)
        If Mnth Mod 2 = 0 Then
            Mnth = Mnth - 1
        End If
        Dim FirstDayBiMonth As DateTime = DateSerial(Year(theDay), Mnth, 1)
        Return FirstDayBiMonth
    End Function