我试图获得一个月和一年的最后一天,但我无法找到我的代码中的错误...
我有这个功能
Private Function Ultim_dia(mes As String) As String
Dim ultim As String
ultim = 1
Select Case (mes)
Case 1, 3, 5, 7, 8, 10, 12
ultim = "31"
Case 4, 6, 9, 11
ultim = "30"
Case 2
If ((mes) Mod 4) = 0 Then
ultim = "29"
Else
ultim = "28"
End If
End Select
Return ultim
End Function
在我的OnclickButton上我有这段代码
Dim ultim As String
ultim = tbMes.Text
Ultim_dia(ultim)
Select Case (ultim)
Case "29"
Me.lb_UltimoDia.Text = "29"
Case "28"
Me.lb_UltimoDia.Text = "28"
Case "30"
Me.lb_UltimoDia.Text = "30"
Case "31"
Me.lb_UltimoDia.Text = "31"
End Select
我不知道我错在哪里,我发现自己不能在任何地方使用我的一年......我不知道这是不是问题
我希望你能帮助我,这让我发疯了......
答案 0 :(得分:3)
要获取当月的天数,请使用DaysInMonth内置功能。</ p>
Dim currentDate As Date = Date.Now
Dim lastDayOfMo = Date.DaysInMonth(currentDate.Year, currentDate.Month)
答案 1 :(得分:1)
给定月份和年份(月份= 10年和年份=&#39; 2014年&#39;)
创建下个月第一天的新日期
Dim q as new date("1", month+1, "2014")
然后做一个负日期的日期加载
dim a as new date = dateadd("d",-1,q)
那应该会给你原始月份的最后日期。
答案 2 :(得分:1)
在VBA中测试过,可能也适用于VB.NET:
Function LastDayOfMonth(theMonth As Long, theYear As Long) As Date
LastDayOfMonth = DateSerial(theYear, theMonth + 1, 0)
End Function
例如,要获得2014年10月的最后一天:
Dim d As Date
d = LastDayOfMonth(10, 2014) ' returns 31 Oct 2014
答案 3 :(得分:0)
按钮单击将调用将返回传递给该函数的年份和月份中的no.of天数的函数。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ultim As String = noOfdays(1989, 8)
End Sub
'<-- function
Public Function noOfdays(ByVal year As Integer, ByVal month As Integer) As String
Try
noOfdays = Date.DaysInMonth(year, month)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Function