Results 1 to 5 of 5

Thread: Extract date from string

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,950

    Extract date from string

    I just have myvar="gi_db_comuni-2026-07-10-b893f.zip"

    how to get date?

    note:
    before and after the date are lenght variable

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,274

    Re: Extract date from string

    Code:
    Sub main()
    Dim myvar As String
    Dim t() As String
    Dim regex As RegExp
    Dim mc As MatchCollection
    Dim m As Match
    Dim mydate As Date
        
        'First way
        myvar = "gi_db_comuni-2026-07-10-b893f.zip"
        t = Split(myvar, "-")
        mydate = DateSerial(CInt(t(1)), CInt(t(2)), CInt(t(3)))
        Debug.Print mydate
        
        'Second way - needs reference to VBScript Regular Expressions
        Set regex = New RegExp
        regex.Pattern = "\.*?-(\d{4}-\d{2}-\d{2})\.*"
        regex.IgnoreCase = True
        regex.Global = True
        Set mc = regex.Execute(myvar)
        If mc.Count = 1 Then
            Set m = mc.Item(0)
            t = Split(m.SubMatches(0), "-")
            mydate = DateSerial(CInt(t(0)), CInt(t(1)), CInt(t(2)))
        End If
    End Sub
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,950

    Re: Extract date from string

    Quote Originally Posted by zvoni View Post
    Code:
    sub main()
    dim myvar as string
    dim t() as string
    dim regex as regexp
    dim mc as matchcollection
    dim m as match
    dim mydate as date
        
        'first way
        myvar = "gi_db_comuni-2026-07-10-b893f.zip"
        t = split(myvar, "-")
        mydate = dateserial(cint(t(1)), cint(t(2)), cint(t(3)))
        debug.print mydate
        
        'second way - needs reference to vbscript regular expressions
        set regex = new regexp
        regex.pattern = "\.*?-(\d{4}-\d{2}-\d{2})\.*"
        regex.ignorecase = true
        regex.global = true
        set mc = regex.execute(myvar)
        if mc.count = 1 then
            set m = mc.item(0)
            t = split(m.submatches(0), "-")
            mydate = dateserial(cint(t(0)), cint(t(1)), cint(t(2)))
        end if
    end sub
    Dim regex As RegExp

    error:
    USED DEFINED NOT DEFINED
    Last edited by luca90; Yesterday at 12:30 PM.

  4. #4
    New Member GustavBrock's Avatar
    Join Date
    Nov 2025
    Location
    Copenhagen
    Posts
    9

    Re: Extract date from string

    Try this:

    Code:
    Public Function ExtractDate(ByVal Value As String) As Date
    
        Dim Start       As Integer
        Dim Text        As String
        Dim FileDate    As Date
        
        For Start = 1 To Len(Value)
            Text = Mid(Value, Start, 10)
            If IsDate(Text) Then
                FileDate = DateValue(Text)
                Exit For
            End If
        Next
        
        ExtractDate = FileDate
    
    End Function

  5. #5
    Fanatic Member
    Join Date
    Nov 2011
    Posts
    810

    Re: Extract date from string

    Quote Originally Posted by luca90 View Post
    Dim regex As RegExp

    error:
    USED DEFINED NOT DEFINED
    Attachment 196254 have you added a reference

    or

    Code:
    Public Function ExtractDate(ByVal Text As String) As String
    
        Dim i As Long
        Dim s As String
    
        For i = 1 To Len(Text) - 9
            s = Mid$(Text, i, 10)
    
            If s Like "####-##-##" Then
                ExtractDate = s
                Exit Function
            End If
        Next i
    
        ExtractDate = ""
    
    End Function

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