Results 1 to 7 of 7

Thread: [RESOLVED] [2008] Date Time Checking Stuff...

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Resolved [RESOLVED] [2008] Date Time Checking Stuff...

    What would be the easiest way, using VB.NET to extract the date and time out of a textbox and check if that time is now. The catch is, that the textbox doesn't necessarily have to have the date included. If the date is now included, then it's assumed to be today's date.

    So like, the input can be "28/01/08 2:17:40 PM", "28/01/08 2:17 PM", "28/01/08 14:17:40", "28/01/08 14:17", "2:17:40 PM", "2:17 PM", "14:17:40" or "14:17".

    The only way i can think to do this would be to pull the string apart piece by piece... Is there an easier way?

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Date Time Checking Stuff...

    Can the textbox contain other string values AND date/time values?
    If it will contain one OR the other, and not both, then DateTime.TryParse would be suitable.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: [2008] Date Time Checking Stuff...

    The textbox will only contain the Date and the Time, or just the Time. No other text.

    Would it be possible to do something like
    If Len(TextboxTxt.Text) => 16 Then
    Split using space as the delimeter and put the AM or PM back on the end if the dimension is larger then 2 (Or assume it's 24 hour time).

    Then how would i go at comparing the date and time with now? I've done this before, but not with date, and it was in VB6, and it's not pretty:

    Code:
    Public Function Process_Time(Input_ As String) As String
    
    Dim hours As String
    Dim minutes As String
    Dim elements() As String
    Dim modes() As String
    
    On Error Resume Next
    modes = Split(Input_, " ")
    elements = Split(Input_, ":")
    If InStr(1, Input_, " ", vbTextCompare) Then elements = Split(modes(0), ":")
    
    hours = elements(0)
    minutes = Trim(elements(1))
    
    If IsNumeric(hours) Then
    If IsNumeric(minutes) Then
    If Val(hours) < 24 Then
    If Val(minutes) < 60 Then
    If Val(hours) > -1 Then
    If Val(minutes) > -1 Then
    
    '24 Hr Time
    If Val(hours) >= 13 Then
    Process_Time = (hours - 12) & ":" & minutes & " PM"
    Else
    If Val(hours) <= 12 Then
    Process_Time = hours & ":" & minutes & " AM"
    End If
    End If
    
    '12 Hr Time
    If Len(modes(1)) >= 1 Then
    
    If UCase(modes(1)) = "PM" Then GoTo Do_12Hr
    If UCase(modes(1)) = "AM" Then GoTo Do_12Hr
    
    If UCase(modes(1)) = "P" Then
    modes(1) = "PM"
    GoTo Do_12Hr
    End If
    
    If UCase(modes(1)) = "A" Then
    modes(1) = "AM"
    GoTo Do_12Hr
    End If
    
    GoTo Dont_12Hr
    
    Do_12Hr:
    Process_Time = hours & ":" & minutes & " " & UCase(modes(1))
    
    Dont_12Hr:
    
    End If
    End If
    End If
    End If
    End If
    End If
    End If
    
    Exit Function
    errorHandler:
    Process_Time = ""
    
    End Function
    I think it's easier to use the functions that already exist.

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Date Time Checking Stuff...

    Have you tried DateTime.TryParse?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: [2008] Date Time Checking Stuff...

    Yes, it works. But now how to extract the value that it redeemed as "True"? Like make it back long again so it doesn't get confused between 24 hour and 12 hour? How to check what it actually worked out, or if 2 times do match?
    Last edited by Slyke; Jan 27th, 2008 at 01:40 PM.

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Date Time Checking Stuff...

    The second argument of TryParse will be passed by reference so it will hold the converted value when after it has returned True.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: [2008] Date Time Checking Stuff...

    Ah yes, got ya. Was wondering why it was changing the textbox. I didn't read. Thank you =). I would give you a rate, but i already have =(.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width