How to compare only month and year? [VB]

跟風遠走 提交于 2021-02-05 12:29:39

问题


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).

The problem is , when i compare two strings

Dim dDate as DateTime

If Not (DateTime.TryParse(txtBox.Text, dDate)) Then
 MessageBox.Show("check date.")
Else
 txtBox.Text = dDate.ToString("MM/yyyy")
end If

IF dDate.ToString("MM/yyyy") < DateTime.Now.ToString("MM/yyyy")
 MessageBox.Show("Below") ' Problem: 03/2024 saying is below than 08/2019
Else
 MessageBox.Show("Above")
End If

Any help?

UPDATE

I CHANGED THE CASE TO

If (dDate.Month AndAlso dDate.Year) < (DateTime.Now.Month AndAlso DateTime.Now.Year) Then
 'input: 07/2019 
 'date expired
Else
'the problem is here
'saying 07/2019 is not < than 08/2019
End If

回答1:


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



回答2:


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



回答3:


Thank you everyone, but the final solutions is made with

If (dDate.Year < DateTime.Now.Year Or (dDate.Year = DateTime.Now.Year And dDate.Month < DateTime.Now.Month)) Then
  'something                
 Else
  'something
End If



回答4:


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.



来源:https://stackoverflow.com/questions/57465328/how-to-compare-only-month-and-year-vb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!