我有该季度第一天和最后一天的vb净代码,但我正尝试在Bi每月周期的第一天和最后一天执行此操作(这两个月结束于2月,4月,6月,8月,10月,十二月)
因此,每两个月的开始和结束日期是 1月1日至2月28日(或29日) 3月1日至4月30日, 5月1日至6月30日, 等等
任何想法如何做到这一点? (以Vb表示感谢)
'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
我走了多远..
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
答案 0 :(得分:0)
这个效果最好
'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