VB中使用datediff计算两个日期间隔时,取非整年度包含月数问题,已有整年代码,求修改

2025-06-28 23:18:07
推荐回答(2个)
回答1:

可以了

Option Explicit
Dim dMonth As Integer

Private Sub Command1_Click()
Dim i As Integer
For i = 0 To txtYear.UBound
txtYear(i).Text = 0
Next
dCount Text1.Text
dCount Text2.Text
End Sub

Private Sub Form_Load()
Dim i As Integer
Text1.Text = "200803-200907"
Text2.Text = "200403-200903"
For i = 0 To txtYear.UBound
txtYear(i).Text = 0
Next
dMonth = 4
End Sub

Private Sub dCount(sStr As String)
Dim kssj As Date, jssj As Date
Dim yf As Integer, i As Integer, nYear As Integer, nMonth As Integer
kssj = Left(sStr, 4) & "-" & Mid(sStr, 5, 2) & "-01"
jssj = Mid(sStr, 8, 4) & "-" & Right(sStr, 2) & "-01"
yf = DateDiff("m", kssj, jssj)+1

nMonth = Month(kssj)
If nMonth < dMonth Then
nYear = Year(kssj) - 1 - 1999
Else
nYear = Year(kssj) - 1999
End If
nMonth = nMonth - 1
For i = 1 To yf
If nMonth = 12 Then
nMonth = 1
Else
nMonth = nMonth + 1
End If
If nMonth = dMonth Then
nYear = nYear + 1
End If
txtYear(nYear).Text = Val(txtYear(nYear).Text) + 1
Next

End Sub

回答2:

很想知道界面是用的上面皮肤控件