|
-
Jul 27th, 2000, 03:50 AM
#1
Thread Starter
Randalf the Red
Well ....
Better still, use the VB IsDate() function. Include your string in this function, which will return True if the string is a valid date and False if not.
Private Sub ..........
Dim MyDateString As String
....Get the date string ....
If IsDate(MyDateString) Then
MsgBox "Gosh! It's a date!"
Else
MsgBox "Sorry, fella, you got it wrong"
Endif
End Sub
This way you don't have to generate and then trap an error.
-
Jul 27th, 2000, 07:59 AM
#2
Member
I am using IsDate() in a program, and while testing it out
that it seems to accept dates that will fit the M/D/Y or D/M/Y format. Since my coding is set up for M/D/Y, is there a way to use/set IsDate() for M/D/Y only.
In other words it will accept 2/16/2000 and 16/2/2000. It
will not accept 16/16/2000.
ANy thoughts?
Tim
VB6, SP4
GT Vengeance
-
Jul 27th, 2000, 08:29 AM
#3
Addicted Member
Don't know why it wont accept it?
the following works for me:
Code:
Dim strDateGood As String
Dim strDateBad As String
strDateGood = "2/16/2000" ' note that I am using d/m/y
strDateBad = "16/16/2000" ' note that I am using d/m/y
MsgBox IsDate(strDateGood)
MsgBox IsDate(strDateBad)
-
Jul 27th, 2000, 09:55 AM
#4
IsDate checks if the passed date is valid in any format. for example if you accept the date in m/d/yy format a test with isdate for 13/2/00 would be valid because it is a valid date in the d/m/yy format.
I use the following function in all my applications.
Private Function ValidDate(strDate As String) As Boolean
Dim intMonth As Integer
Dim intDay As Integer
Dim intYear As Integer
If Len(Trim$(strDate)) < 8 Then
ValidDate = True
Exit Function
End If
intMonth = Int(Left(strDate, 2))
intDay = Int(Mid(strDate, 3, 2))
intYear = Int(Right(strDate, 4))
ValidDate = True
If intMonth > 12 Or intDay > 31 Then
ValidDate = False
Exit Function
End If
If (intMonth = 2 Or intMonth = 4 Or intMonth = 6 Or intMonth = 9 Or intMonth = 11) And intDay > 30 Then
ValidDate = False
Exit Function
End If
If (intMonth = 2) And (intDay = 30) Then
If (intYear Mod 100 = 0) And (intYear Mod 400) = 0 Then
ValidDate = True
Else
ValidDate = False
Exit Function
End If
End If
End Function
-
Jul 27th, 2000, 09:59 AM
#5
sorry, I forgot to mention that in the above code there are no slashes and the format is strictly mmddyyyy . I all my text boxes, I do not allow any character except 0-9.
The code
If Len(Trim$(strDate)) < 8 Then
ValidDate = True
Exit Function
End If
should read
If Len(Trim$(strDate)) < 8 Then
ValidDate = False
Exit Function
End If
-
Jul 27th, 2000, 01:48 PM
#6
Member
Thanks, I will give it a shot!
Tim
VB6, SP4
GT Vengeance
-
Jul 27th, 2000, 03:22 PM
#7
Hyperactive Member
I do not use IsDate()
Normally I am not interested in the result of isDate because it is defined thus:
"Returns a Boolean value indicating whether anexpression can be converted to a date."
I am not interested if a string CAN be converted but whether it IS converted. CDate converts according to your regional settings (see the date / time section).
IsDate("16/7/2000") returns True
IsDate("7/16/2000") returns True
CDate("16/7/2000") works
CDate("7/16/2000") fails
This is because I use D/M/Y as my date format (Standard in my country). If you want to validate a date, you have to either do it manually OR use CDate.
Regards
Paul Lewis
-
Jul 27th, 2000, 03:31 PM
#8
So Unbanned
Originally posted by TimC
I am using IsDate() in a program, and while testing it out
that it seems to accept dates that will fit the M/D/Y or D/M/Y format. Since my coding is set up for M/D/Y, is there a way to use/set IsDate() for M/D/Y only.
In other words it will accept 2/16/2000 and 16/2/2000. It
will not accept 16/16/2000.
ANy thoughts?
*Laughs*
16 isn't a month, it's a day. 2000 a year, neither day or month.
Since it can't find a month, it's not a date!
-
Jul 27th, 2000, 03:39 PM
#9
Member
The point was that it accepted either m/d/y or d/m/y
and did not accept a string that did not match that,
like 16/16/2000, because one of the numbers could not
be a day. That is what it was supposed to do. However I needed something which only accepted m/d/y, which
sarun graciously offered.
*Shakes head*
Tim
VB6, SP4
GT Vengeance
-
Jul 27th, 2000, 04:03 PM
#10
Hyperactive Member
Sulking in corner...
hehe,
Just kidding, even though I feel CDate is the way to go, I thought I would point out that sarun does not handle leap years properly 
You might want to fix that unless you don't mind the fault 
Cheers
Paul Lewis
-
Jul 27th, 2000, 04:19 PM
#11
Thanks for pointing out!
this should handle leap years better -
If (intMonth = 2) And (intDay = 30) Then
If intYear Mod 4 <> 0 Then
ValidDate = False
Exit Function
End If
If intYear Mod 100 = 0 And Not (intYear Mod 400 = 0) Then
ValidDate = False
End If
End If
-
Jul 27th, 2000, 04:56 PM
#12
Hyperactive Member
Better...
Sarun,
Better, but I don't think it is yet perfect. I could be wrong of course, but please double check the limit on the number of days in month 2. You current check if the value is 30 which is impossible in the calendar I use 
I think you really want to check for the day = 29 which is "leap day". Normall there are only 28 days in february...
Cheers
Paul Lewis
-
Jul 27th, 2000, 08:48 PM
#13
Hyperactive Member
Well Sarun ...
on error goto WrongDate
MyDate = CDate(AnyString)
on error gotot 0
That will work in any cases (including leap year.
srl !
as soon as it is a case for you a specific format then before my codes you have to use Format function to adjust string to a date format of youe country.
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
|