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

I believe that my question is not understood:

My line (uuttcc) contains the UTC time stamp format created on another page using JAVASCRIPT.

I want to point out again that my line contains exactly that: Wed, 10 Jan 2018 17:23:34 UTC

Is there any way to break this โ†’ ( Wed, 10 Jan 2018 17:23:34 UTC ) into this โ†’ ( YYYY/MM/DD )?


I have a string ( uuttcc ) that contains the UTC date / time.

:

 <% Response.Write(uuttcc) %> 

Providing me with the following result:

Wed, 10 Jan 2018 17:23:34 UTC

Is there any special way to split this line into 3 parts, like DD YY MM in classic ASP / VBScript?

+2
vbscript asp-classic
source share
2 answers

Not going to answer, but with all the fluffs 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 

How about not using Split() ?

The main problem is to get the string that CDate() will recognize after using it using the built-in date functions , will allow you to create whatever format you want.

In fact, only what breaks this particular parsing using CDate() is 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 classic asp format
  • Does VBScript have the equivalent of DateTime.TryParse?
  • date format in VBS
  • Format current date and time
+1
source share

Use Split () to get parts of your input string. Load the correct parts into DateSerial () / CDate () to get a Date that should display / print as / d / m / y if it suits your language requirements / regional settings. If you do not need a date, create the desired String through Join (). How in:

 Option Explicit Function mkDicMonth() Dim dicT : Set dicT = CreateObject("Scripting.Dictionary") Dim i For i = 1 To 12 dicT(MonthName(i, True)) = i Next Set mkDicMonth = dicT End Function Dim sInp : sInp = "Wed, 10 Jan 2018 17:23:34 UTC" Dim dicM : Set dicM = mkDicMonth() Dim aParts : aParts = Split(sInp) Dim sOtp : sOtp = Join(Array(aParts(1), dicM(aParts(2)), aParts(3)), "/") WScript.Echo TypeName(sOtp), sOtp Dim dtOtp ' DateSerial dtOtp = DateSerial(CInt(aParts(3)), CInt(dicM(aParts(2))), CInt(aParts(1))) WScript.Echo 1, TypeName(dtOtp), dtOtp, "(german locale, dmy)" ' CDate (risky, order, locale dependent) dtOtp = CDate(sOtp) WScript.Echo 2, TypeName(dtOtp), dtOtp, "(german locale, dmy)" ' CDate (risky, monthname, locale dependent) dtOtp = CDate(Join(Array(aParts(1), aParts(2), aParts(3)))) WScript.Echo 3, TypeName(dtOtp), dtOtp, "(german locale, dmy)" 

exit:

 cscript 48193001.vbs String 10/1/2018 1 Date 10.01.2018 (german locale, dmy) 2 Date 10.01.2018 (german locale, dmy) 3 Date 10.01.2018 (german locale, dmy) 
0
source share

All Articles