How to compare only month and year? [VB]

前端 未结 4 806
伪装坚强ぢ
伪装坚强ぢ 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-29 12:52

    Using date values is probably best, but if string comparisons must be made.

    Dim dDate as DateTime
    
    If Not (DateTime.TryParse(txtBox.Text, dDate)) Then
      MessageBox.Show("check date.")
    Else
      txtBox.Text = dDate.ToString("yyyyMM")
    End If
    
    If dDate.ToString("yyyyMM") < DateTime.Now.ToString("yyyyMM") Then
      MessageBox.Show("Below") 
    ElseIf dDate.ToString("yyyyMM") > DateTime.Now.ToString("yyyyMM") Then
      MessageBox.Show("Above")
    Else
      MessageBox.Show("Same")
    End If
    

提交回复
热议问题