#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    How to validate a string is earlier than Today's date?


    Hi,

    I am pretty new to VB scripting and have been trying to pick it up from help files and information on this site.

    I am trying to put together a script that verifies a 12 digit string inputted into an input box.

    The checks that I want to perform on the input value are as follows:

    1. Check string to be all numeric
    2. Check string to be 12 characters long
    3. Check that the first 8 digits of the string represent a valid date
    4. And check that the first 8 digits (which represent a date) are not older than the current date.

    I have successfully got the first three points working, but cannot figure out how to script point 4.

    #
    Dim newSubject As String
    Dim newSubset As String
    Dim newSubset2 As String
    Dim newSubset3 As String
    Dim newSubset4 As String
    Dim newDatestr As String
    Dim newDateStr2 As Date
    Dim okString As Boolean

    Dim ThisDate As Date = Today

    newDatestr = Format(ThisDate, "ddmmyyyy")

    objItem = GetCurrentItem()
    objMail = objItem.Forward

    Do
    okString = True
    newSubject = InputBox("Vertagsnummer:")

    If newSubject = "" Then Exit Sub

    newSubset = Mid(newSubject, 1, 2)
    newSubset2 = Mid(newSubject, 3, 2)
    newSubset3 = Mid(newSubject, 5, 4)
    newSubset4 = Mid(newSubject, 1, 8)

    newDateStr2 = CDate(newSubset4)

    If Not Len(newSubject) = 12 Then okString = False
    If Not IsNumeric(newSubject) Then okString = False
    If Not (newSubset >= "01" And newSubset <= "31") Then okString = False
    If Not (newSubset2 >= "01" And newSubset2 <= "12") Then okString = False
    If Not (newSubset3 > "1993") Then okString = False
    If Not (newDateStr2 <= ThisDate) Then okString = False

    If Not okString Then MsgBox("You did not enter a correct value. Please try again")

    Loop Until okString = True
    #

    I can't seem to be able to get the CDate function working when I try to convert the string of numbers (NewSubset4).

    Any help or guidance is much appreciated.

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    45
    It is actually a lot simpler than you might think. Dates in the computer are stored as Double Precision numbers, with the number to the left of the decimal place representing the number of days offset from a base date (12/30/1899), and the number to the right of the decimal place representing the number of seconds since midnight. File dates are actually stored in Greenwich Mean Time (GMT) and converted to the local date/time when displayed.
    ?now, cdbl(now), int(cdbl(now))
    09/20/13 7:29:39 AM 41537.3122569444 41537
    Code:
        Dim newSubject As String
        Dim newDateStr2 As Date
    TryAgain:
        newSubject = InputBox("Vertagsnummer:")
        If Not IsNumeric(newSubject) Or Len(newSubject) <> 12 Then
            GoTo TryAgain
        Else
            newDateStr2 = Left$(newSubject, 2) & "/" & Mid$(newSubject, 3, 2) & "/" & Mid$(newSubject, 5, 4)
            If Int(CDbl(Now)) = CDbl(newDateStr2) Then
                MsgBox (newDateStr2 & " Matches Today's Date!")
            Else
                MsgBox (newDateStr2 & " Does not Match Today's Date!")
            End If
        End If
    Since this is VB6 code, it may not be exactly the same in VB Script, but you get the idea.

    J.A. Coutts
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0
    Thank you. I will give this a go.



    Originally Posted by couttsj
    It is actually a lot simpler than you might think. Dates in the computer are stored as Double Precision numbers, with the number to the left of the decimal place representing the number of days offset from a base date (12/30/1899), and the number to the right of the decimal place representing the number of seconds since midnight. File dates are actually stored in Greenwich Mean Time (GMT) and converted to the local date/time when displayed.
    ?now, cdbl(now), int(cdbl(now))
    09/20/13 7:29:39 AM 41537.3122569444 41537
    Code:
        Dim newSubject As String
        Dim newDateStr2 As Date
    TryAgain:
        newSubject = InputBox("Vertagsnummer:")
        If Not IsNumeric(newSubject) Or Len(newSubject) <> 12 Then
            GoTo TryAgain
        Else
            newDateStr2 = Left$(newSubject, 2) & "/" & Mid$(newSubject, 3, 2) & "/" & Mid$(newSubject, 5, 4)
            If Int(CDbl(Now)) = CDbl(newDateStr2) Then
                MsgBox (newDateStr2 & " Matches Today's Date!")
            Else
                MsgBox (newDateStr2 & " Does not Match Today's Date!")
            End If
        End If
    Since this is VB6 code, it may not be exactly the same in VB Script, but you get the idea.

    J.A. Coutts

IMN logo majestic logo threadwatch logo seochat tools logo