UTC time stamp format - Split in parts a string containing the UTC time date (classic asp/vbscript)

后端 未结 2 674
无人共我
无人共我 2021-01-21 10:02

I believe that my question is misunderstood:

My string (uuttcc) contains a UTC time stamp format, created in other page with JAVASCRIPT.

2条回答
  •  猫巷女王i
    2021-01-21 10:27

    Wasn't going to answer but with all the fluff in the comments, use Split().

    Dim input: input = "Wed, 10 Jan 2018 17:23:34 UTC"
    Dim parsed: parsed = ParseDateString(input)
    Dim output: output = Year(parsed) & "/" & Right("00" & Month(parsed), 2) & "/" & Right("00" & Day(parsed), 2)
    Call Response.Write(output)
    
    Function ParseDateString(value)
        'Split the string into manageable chunks.
        Dim parts: parts = Split(value, Chr(32))
        Dim dt, tm
        'Check the split created an array we can work with.
        If IsArray(parts) Then
            'Ignore the first element and use elements 1 - 3 to 
            'create the date structure.
            dt = parts(1) & Chr(32) & parts(2) & Chr(32) & parts(3)
            'Use the 5th element for the time structure.
            tm = parts(4)
            'Stitch them together to form a date variable.
            dt = CDate(dt & Chr(32) & tm)
        Else
            'We don't have a valid format return an empty string.
            dt = Empty
        End If
        ParseDateString = dt
    End Function
    

    Output:

    2018/01/10
    

    What about not using Split()?

    The main issue is getting the string to be recognised by CDate() once you have this using inbuilt date functions will allow you to build whatever format you want.

    In fact the only thing that breaks that particular parse using CDate() is the Wed, and UTC, so you ignore them using a combination of Mid(), InStr() and Len() as in this compact example;

    Dim input: input = "Wed, 10 Jan 2018 17:23:34 UTC"
    Dim output: output = CDate(Mid(input, InStr(1, input, ",") + 1, ((Len(input) - 4) - InStr(1, input, ","))))
    'You now have a valid `Date` variable you can manipulate to your hearts
    'content.
    Call Response.Write(output)
    
    10/01/2018 17:23:34
    

    Useful Links

    • How to convert string to datetime format classic asp
    • Does VBScript have a DateTime.TryParse equivalent?
    • date format in VBS
    • Format current date and time

提交回复
热议问题