Results 1 to 4 of 4

Thread: Extract date from string

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,954

    Extract date from string

    I just have myvar="Fri, 05 Jun 2026 08:36:39 GMT"

    how to transform in:

    dt=05/06/2026

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

    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 = "Fri, 05 Jun 2026 08:36:39 GMT"
        t = Split(myvar, " ")
        mydate = CDate(t(1) & " " & t(2) & " " & t(3))
        Debug.Print mydate 'Prints MyDate in anyone's locale
        
        'Second way - needs reference to VBScript Regular Expressions
        Set regex = New RegExp
        regex.Pattern = ".*?(\d{2} [a-zA-Z]{3} \d{4}).*"
        regex.IgnoreCase = True
        regex.Global = True
        Set mc = regex.Execute(myvar)
        If mc.Count = 1 Then
            Set m = mc.Item(0)
            mydate = CDate(m.SubMatches(0))
            Debug.Print mydate 'Prints MyDate in anyone's locale
        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
    Member
    Join Date
    Jul 2024
    Posts
    41

    Re: Extract date from string

    use this

    Code:
    Public Function GetIt1Cached(vSource As String, vIndex As Integer, Optional vSep As String = vbTab) As String
        Static vStrings() As String '2026-07-03
        Static PrevSource As String
        
        If InStr(vSource, vSep) > 0 Then
            If vSource <> PrevSource Then
                PrevSource = vSource
                vStrings = Split(vSource, vSep)
            End If
            If vIndex > UBound(vStrings) Then
                Exit Function
            End If
            GetIt1Cached = vStrings(vIndex)
            If Len(GetIt1Cached) > 200 Then
                GetIt1Cached = Trim(GetIt1Cached)
            End If
        End If
    End Function 'tks to wqweto
    
    private function Month2Num(vMonth as string) as integer
        Month2Num = (InStr("jan feb mar apr may jun jul aug sep oct nov dic", Left(LCase(vMonth), 3)) + 3) / 4
    End Function
    
    sub main()
        dim dt as string
        Dim myvar  as string
        myvar="Fri, 05 Jun 2026 08:36:39 GMT"
        dt = GetIt1Cached(myvar, 1, " ") + "/" + format(month2num(GetIt1Cached(myvar, 2," ")), "00") + "/" + GetIt1Cached(myvar, 3, " ")
    end sub

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,954

    Re: Extract date from string

    Quote Originally Posted by prote01 View Post
    use this

    Code:
    public function getit1cached(vsource as string, vindex as integer, optional vsep as string = vbtab) as string
        static vstrings() as string '2026-07-03
        static prevsource as string
        
        if instr(vsource, vsep) > 0 then
            if vsource <> prevsource then
                prevsource = vsource
                vstrings = split(vsource, vsep)
            end if
            if vindex > ubound(vstrings) then
                exit function
            end if
            getit1cached = vstrings(vindex)
            if len(getit1cached) > 200 then
                getit1cached = trim(getit1cached)
            end if
        end if
    end function 'tks to wqweto
    
    private function month2num(vmonth as string) as integer
        month2num = (instr("jan feb mar apr may jun jul aug sep oct nov dic", left(lcase(vmonth), 3)) + 3) / 4
    end function
    
    sub main()
        dim dt as string
        dim myvar  as string
        myvar="fri, 05 jun 2026 08:36:39 gmt"
        dt = getit1cached(myvar, 1, " ") + "/" + format(month2num(getit1cached(myvar, 2," ")), "00") + "/" + getit1cached(myvar, 3, " ")
    end sub
    tks bro

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