Results 1 to 13 of 13

Thread: Testing for a valid date and or time

  1. #1

    Thread Starter
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    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.
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  2. #2
    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

  3. #3
    Addicted Member
    Join Date
    Jul 2000
    Location
    Scotland
    Posts
    184
    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)

  4. #4
    Guest
    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

  5. #5
    Guest
    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



  6. #6
    Thanks, I will give it a shot!
    Tim

    VB6, SP4
    GT Vengeance

  7. #7
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    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

  8. #8
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    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!

  9. #9
    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

  10. #10
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    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

  11. #11
    Guest
    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

  12. #12
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    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

  13. #13
    Hyperactive Member
    Join Date
    Jun 1999
    Posts
    308
    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
  •  



Click Here to Expand Forum to Full Width