How to compare only month and year? [VB]

前端 未结 4 840
伪装坚强ぢ
伪装坚强ぢ 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 13:15

    Convert the year and month to Integer values, which are easy to compare less-than or greater-than. For example, if you had two DateTimePicker controls on a form, named dtpDate1 and dptDate2, and a button btnTest:

    Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click
    
        Dim date1 As Integer = (dtpDate1.Value.Year * 100) + dtpDate1.Value.Month
        Dim date2 As Integer = (dtpDate2.Value.Year * 100) + dtpDate2.Value.Month
    
        MsgBox("Date1 = " & date1.ToString & vbCrLf & "Date2 = " & date2.ToString)
    
        If date1 = date2 Then MsgBox("Equal!")
        If date1 < date2 Then MsgBox("date1 is less than date2")
        If date1 > date2 Then MsgBox("date1 is greater than date2")
    
    End Sub
    

    The dates are converted to integers in a YYYYMM format. This is a data warehouse technique to convert dates to integers for better query performance when date is used often in a WHERE clause.

提交回复
热议问题