|
-
Jan 27th, 2008, 11:20 AM
#1
Thread Starter
Fanatic Member
[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?
-
Jan 27th, 2008, 11:46 AM
#2
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.
-
Jan 27th, 2008, 12:03 PM
#3
Thread Starter
Fanatic Member
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.
-
Jan 27th, 2008, 12:14 PM
#4
Re: [2008] Date Time Checking Stuff...
Have you tried DateTime.TryParse?
-
Jan 27th, 2008, 01:35 PM
#5
Thread Starter
Fanatic Member
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.
-
Jan 27th, 2008, 01:42 PM
#6
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.
-
Jan 27th, 2008, 01:47 PM
#7
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|