Results 1 to 2 of 2

Thread: Extract date from string

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,953

    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

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