How to compare only month and year? [VB]

前端 未结 4 807
伪装坚强ぢ
伪装坚强ぢ 2021-01-29 12:23

Its quite simple, i just want to compare two dates using month and year, if the input date (mont and year only) are above or below that current date (month and year).

Th

4条回答
  •  佛祖请我去吃肉
    2021-01-29 12:59

    I'd avoid using strings.

        Dim dDate As DateTime
        If Not (DateTime.TryParse(txtBox.Text, dDate)) Then
            'bad date
            MessageBox.Show("check date.")
        Else
            Select Case dDate.Year
                Case Is < DateTime.Now.Year
                    MessageBox.Show("Below")
                Case Is > DateTime.Now.Year
                    MessageBox.Show("Above")
    
                Case Else 'years are equal,check month
                    Select Case dDate.Month
                        Case Is < DateTime.Now.Month
                            MessageBox.Show("Below")
                        Case Is > DateTime.Now.Month
                            MessageBox.Show("Above")
                        Case Else 'equal months
                            MessageBox.Show("SAME") '????
                    End Select
            End Select
        End If
    

提交回复
热议问题