Click here to Skip to main content
14,691,771 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have this code that compare a date from gridview to date.today
but it seems not working


"select * from out_logtable  order by outid desc limit 1")
DataGridView1.DataSource = Me.BindingSource1
DataGridView1.AutoGenerateColumns = True
Dim x As String = DateAndTime.Today.ToString("d")
Dim y As String = DataGridView1.Rows(0).Cells(2).Value.ToString
If DataGridView1.Rows(0).Cells(1).Value <> "admin" Then
    If x.ToString > y.ToString Then
        ExecuteSQL(1, "Update login_table set hour = '2' ")
    End If
Else

End If
Posted
Updated 8-Jun-20 9:35am

Compares two dates?? No you don't. You have to code that compares two STRINGS, not dates. The comparison is not the same.

Convert the values to DateTime, not Strings.
   
Comments
   
5, but the answer needs to be continued; I've done it in Solution 2. :-)
—SA
Dave Kreskowiak 7-Oct-14 7:27am
   
Yeah I was short on time.
Maciej Los 7-Oct-14 1:55am
   
Good point, a 5!
To continue Solution 1:

…and then compare instanced of the structure System.DateTime using the operator '=' or '<>' which means "same moment of time" or "not the same moment of time".
Note that you can also use '<' (sooner than), '>' (later than), '>=' (not sooner than), '<=' (not later than).

Please see: http://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx[^].

Also, I don't know what is your binding source, but if you use a database, don't store data related to time as string data. For these purposes, there are special data types, such as DATE.

—SA
   
v4
Comments
Maciej Los 7-Oct-14 2:15am
   
It's VB.NET, not C#. Simple '=' would be enough.
   
Ah... well, thank you. Fixed.
—SA
Maciej Los 7-Oct-14 2:21am
   
Now, it's correct. 5ed!
Please, see my answer.
   
Thank you, Maceij, especially for a fix (one more fix is C# "!=").
—SA
VB.NET 'knows' Date data type[^]. It holds dates from January 1 of the year 0001 through December 31 of the year 9999, and times from 12:00:00 AM (midnight) through 11:59:59.9999999 PM.

Example 1 - using simple '=' operator:
Dim a As Date = CDate("2014-09-12"), b As Date = Date.Now

If a = b Then
    Console.WriteLine("Dates are equal {0}={1}", a, b)
Else
    Console.WriteLine("Dates aren't equal {0}<>{1}", a, b)
End If

Console.ReadKey()


Example 2 - using Compare method[^]:
Dim date1 As Date = #08/01/2009 12:00AM#
Dim date2 As Date = #08/01/2009 12:00PM#
Dim result As Integer = DateTime.Compare(date1, date2)
Dim relationship As String

If result < 0 Then
   relationship = "is earlier than"
ElseIf result = 0 Then
   relationship = "is the same time as"
Else
   relationship = "is later than"
End If

Console.WriteLine("{0} {1} {2}", date1, relationship, date2)


Example 3 - using DateDiff function[^]:
Dim firstDate, msg As String
Dim secondDate As Date
firstDate = InputBox("Enter a date")
Try
    secondDate = CDate(firstDate)
    msg = "Days from today: " & DateDiff(DateInterval.Day, Now, secondDate)
    MsgBox(msg)
Catch
    MsgBox("Not a valid date value.")
End Try



Note: As is explained here: Data Type Summary (Visual Basic)[^], Date data type uses DateTime structure - to put it simply.
   
Comments
   
5ed. I would prefer to avoid VB-specific API though and keep to .NET BCL whenever possible, and then FCL.
—SA
Maciej Los 7-Oct-14 11:08am
   
Thank you, Segey ;)
Could you be so kind and explain the meanings of words: BCL and FCL? As far i know BCL is Base Class Library, but FCL - i never meet this statement before, as far as i remember.
   
Sure. Base Class Library, Framework Class Library (wider, BCL + what is not part of the ECMA standard) — please see the terms in Wikipedia.
—SA
Maciej Los 7-Oct-14 11:30am
   
I wasn't sure the meaning of FCL, because i found more than one meaning for it.
Thank you for clarification, Sergey.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900