VB.NET - compare user date and time with system date and time

Asked By jenn on 15-Nov-11 08:20 AM
good morning guys.

i have a problem in trying to figure out how to compare a user entered date and time with the system date and time.
what i WANT to have happen is this:

it is a reminder program that holds a date/time picker for the user to select a date- and then a textbox for the user to type in the time of the reminder. this information is then stored into a listview, which is SUPPOSE to (lol) shoot up a message box when the system hits that particular date and time.

every part of this project is working fine- EXCEPT it doesnt work LOLLLLLLLLLLL- meaning it will not shoot me my message box :(

i just dont get it, and i am really hoping SOMEONE can tell me what the hell i am doing(or not doing...)

here is the run down of controls:
a date/time picker- allows the user to select the DATE
a textbox- allows the user to type in the TIME
a textbox- allowing the user to type in what the messagebox will say when it comes up-
and a listview to store the reminders in

also have a label that shows the current date and a label that shows the current time on the form

OKAY--- here is the ENTIRE code block for the reading of my listview and where i am trying to get the messagebox to show. i have a pre-generated message in the message box right now as a test, (lol...a test that has not worked yet!:

Private Sub ReadReminderList()
  If Not System.IO.File.Exists(Application.StartupPath & "\ReminderList.xml") Then Exit Sub
  Dim doc As New XmlDocument
  Try
    doc.Load(Application.StartupPath & "\ReminderList.xml")
    For Each node As XmlNode In doc.SelectNodes("//Item")
  
    Dim item As New ListViewItem
    Dim nodeAttributes As XmlAttributeCollection = node.Attributes
    item.Text = nodeAttributes(0).InnerText  'Date
    item.SubItems.Add(nodeAttributes(1).InnerText)  'Time
    item.SubItems.Add(nodeAttributes(2).InnerText)    'Name
  
    If nodeAttributes(0).InnerText.Equals(Date.Now) AndAlso nodeAttributes(1).InnerText.Equals(TimeOfDay) Then
      MsgBox("working")
       
      lvRemList.Items.Add(item)
    End If
    Next
    doc = Nothing
  Catch ex As Exception
    lvRemList.Items.Clear()
  
  End Try
  
  End Sub
Robbe Morris replied to jenn on 15-Nov-11 08:39 AM
You are comparing "strings" and not DateTime.  DateTime.Now.ToString() (which is what DateTime.Now will be converted to by the compiler in your code) will yield a specific date/time format based on your cultural settings.

You'll want to take these to strings and convert them to the type DateTime prior to comparison.
Reena Jain replied to jenn on 15-Nov-11 08:51 AM
Hi,

you can use CompareValidator
eg
<asp:CompareValidator ID="CompareValidator1" ControlToValidate="txt_DOB" Operator="LessThan" Type="Date" runat="server" ErrorMessage="Entered date must be less than current date" ValidationGroup="Add"></asp:CompareValidator>

//same validation group should be added in the button control in which click the validation should be called
 
//You should also write the below code in page load
CompareValidator1.ValueToCompare = DateTime.Now.ToString("MM/dd/yyyy");

this is the RangeValidator eg

<asp:RangeValidator ID="RangeValidator2" runat="server"
ControlToValidate="txtedate" CultureInvariantValues="true"
ErrorMessage="End Date is greater than Current Date" ValidationGroup="Add"
Type="Date" MinimumValue="2010-01-01" MaximumValue="2010-12-31">
</asp:RangeValidator>

or
Change the database date to format dd/mm/yyyy using myDateTime.ToShortDateString() so that both the dates will be in same format.
then :
if (DateTime.Compare(date1, date2) > 0) then
// which means ("date1 > date2")
if (DateTime.Compare(date1, date2) == 0) then
//which means ("date1 == date2")
if (DateTime.Compare(date1, date2) < 0) then
//which means ("date1 < date2")


Hope this will help you
jenn replied to Robbe Morris on 15-Nov-11 10:35 AM
okay, these are the changes made to the code block- and i still dont have it right, as there is still no message box! *sigh*

any ideas???

Private Sub ReadReminderList()
     If Not System.IO.File.Exists(Application.StartupPath & "\ReminderList.xml") Then Exit Sub
     Dim doc As New XmlDocument
     Try
       doc.Load(Application.StartupPath & "\ReminderList.xml")
       For Each node As XmlNode In doc.SelectNodes("//Item")
         Dim item As New ListViewItem
         Dim nodeAttributes As XmlAttributeCollection = node.Attributes
         item.Text = nodeAttributes(0).InnerText  'Date
         item.SubItems.Add(nodeAttributes(1).InnerText)  'Time
         item.SubItems.Add(nodeAttributes(2).InnerText)    'Name
         If nodeAttributes.Count > 4 Then
           If nodeAttributes(0).Value = datToDo.Text Then
             Dim test As DateTime
             Dim _day As String = nodeAttributes(0).InnerText.Substring(nodeAttributes(0).InnerText.IndexOf("/") + 1, 2)
             Dim _month As String = nodeAttributes(0).InnerText.Substring(0, nodeAttributes(0).InnerText.IndexOf("/"))
             Dim _year As String = nodeAttributes(0).InnerText.Substring(nodeAttributes(0).InnerText.IndexOf(_day) + 3, 4)
             If DateTime.TryParse(New DateTime(CInt(_year), CInt(_month), CInt(_day)), test) Then
               If DateTime.Compare(test, DateTime.Now) Then
                 MsgBox("It Works")
               End If
             End If
           End If
         End If
         lvRemList.Items.Add(item)
       Next
       doc = Nothing
     Catch ex As Exception
       lvRemList.Items.Clear()
     End Try
   End Sub