看了别人写的C#的 自己转了一下 然后后重新改了改 写成了这个
另外还有一个我写的验证日期是否合法的代码 在后面 都是vb的 c#只会看不会写
'判断闰年======================= Private Function CheckLeap(ByVal year As Integer) As Boolean If (year Mod 4 = 0) AndAlso (year Mod 100 <> 0) OrElse (year Mod 400 = 0) Then Return True Else Return False End If End Function '判断闰年结束======================= '绑定每月的天数===================== Private Sub BindDays(ByVal year As Integer,ByVal month As Integer) Dim i As Integer Dim AlDay As New ArrayList() Select Case month Case 1,3,5,7,8,10,12 For i = 1 To 31 AlDay.Add(i) Next 'Exit Select Case 2 If CheckLeap(year) Then For i = 1 To 29 AlDay.Add(i) Next Else For i = 1 To 28 AlDay.Add(i) Next End If 'Exit Select Case 4,6,9,11 For i = 1 To 30 AlDay.Add(i) Next 'Exit Select End Select DropDownList_day.DataSource = AlDay DropDownList_day.DataBind() End Sub '绑定每月的天数结束===================== '页面上拖三个DropDownList DropDownList_year DropDownList_month DropDownList_day 都要autopostback Protected Sub Page_Load(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Load Dim AlYear As New ArrayList() Dim i As Integer For i = 1940 To 1992 AlYear.Add(i) Next Dim AlMonth As New ArrayList For i = 1 To 12 AlMonth.Add(i) Next If Not Me.IsPostBack Then DropDownList_year.DataSource = AlYear DropDownList_year.DataBind() DropDownList_year.SelectedValue = 1985 '绑定年 DropDownList_month.DataSource = AlMonth DropDownList_month.DataBind() DropDownList_month.SelectedValue = 6 '绑定月 Dim year As Integer,month As Integer year = Int32.Parse(DropDownList_year.SelectedValue) month = Int32.Parse(DropDownList_month.SelectedValue) BindDays(year,month) '绑定天 DropDownList_day.SelectedValue = 15 End If End Sub Protected Sub DropDownList_year_SelectedIndexChanged(ByVal sender As Object,ByVal e As System.EventArgs) Handles DropDownList_year.SelectedIndexChanged Dim year As Integer,month As Integer year = Int32.Parse(DropDownList_year.SelectedValue) month = Int32.Parse(DropDownList_month.SelectedValue) BindDays(year,month) End Sub Protected Sub DropDownList_month_SelectedIndexChanged(ByVal sender As Object,ByVal e As System.EventArgs) Handles DropDownList_month.SelectedIndexChanged Dim year As Integer,month) End Sub
'验证日期合法=============================== '里面的1992 1940是我设置的年份范围 '页面上有个Label_false提示哪里的错误 '用到了上面代码里的判断闰年函数CheckLeap If (yy > 1992 Or (yy < 1940)) Or ((mm < 1) Or (mm > 12)) Then Label_false.Text = "出生年月错误,请您再试一下!" Return End If Select Case mm Case 1,12 If dd > 31 Or dd < 1 Then Label_false.Text = "出生日期错误,请您再试一下!" Return End If Case 4,11 If dd > 30 Or dd < 1 Then Label_false.Text = "出生日期错误,请您再试一下!" Return End If Case 2 If CheckLeap(yy) = False Then If dd > 28 Or dd < 1 Then Label_false.Text = "出生日期错误,请您再试一下!" Return End If Else If dd > 29 Or dd < 1 Then Label_false.Text = "出生日期错误,请您再试一下!" Return End If End If End Select '验证完毕 下面转成正常的Date类型的dateout Dim yymmdd As String = yy & "-" & mm & "-" & dd Dim dateout As Date = DateTime.Parse(yymmdd)