2

I believe that my question is misunderstood:

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

I want to point out again, that my string contain exactly this: Wed, 10 Jan 2018 17:23:34 UTC

Is there any way to break apart this--> (Wed, 10 Jan 2018 17:23:34 UTC) to that--> (YYYY/MM/DD) ?


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

The:

<%
Response.Write(uuttcc)
%>

Giving me the following result:

Wed, 10 Jan 2018 17:23:34 UTC

Is there any particular way to split this string in 3 parts like DD YY MM in classic ASP/VBScript??

Freddakos
  • 97
  • 1
  • 11
  • 2
    `Datepart` ???? – ACatInLove Jan 10 '18 at 18:35
  • @ Lankymart, Is there any particular reason that all of you blocked my question as duplicate??? Your reference to the similar (https://stackoverflow.com/questions/22574092/format-current-date-and-time) answered question it has nothing to do with my question. My question was very clean and simple: I am trying to split this Wed, 10 Jan 2018 17:23:34 UTC exactly as is written And not the ''now'' that you answered in the reference article. – Freddakos Jan 11 '18 at 13:52
  • @lankymart They missed it's a string parsing problem, not a date problem, as it appears on a quick read (as did I the first time I read it). Use `Arr()=Split(Str," ")` and access each element in the array. And you can do it more than once if you say want the seconds. – ACatInLove Jan 11 '18 at 13:58
  • @ACatInLove - Thank very much for your attention! Can you please help me a little bit more… The name of my string is ‘’uuttcc’’ and no, I don’t need seconds – Freddakos Jan 11 '18 at 14:12
  • 1
    You cannot program without Help. Download here http://download.microsoft.com/download/winscript56/Install/5.6/W982KMeXP/EN-US/scrdoc56en.exe and search for the `Split` function. So first line. `Dim MyArr=Split(uuttcc, " ")` To print out the day and keeping the comma. `Msgbox MyArr(0)`. – ACatInLove Jan 11 '18 at 14:20
  • @ACatInLove I didn't miss nothing, its got nothing to do with parsing a string if they use `CDate()` to parse the string as a date then pull out the various date parts using in built functions. But then you'd know that if you used VBScript. – user692942 Jan 11 '18 at 17:51
  • @user2986570 let me put it this way... You think you're the first person who has asked how to parse a string as a date in the years people have been asking questions on [so] about VBScript? It's VBScript 101 go back read [ask] and you will find the answer. You don't even need `Split()` in this instance and even if you did, it's not difficult and there are plenty of questions about splitting and parsing date strings. You've heard of `CDate()` right? – user692942 Jan 11 '18 at 17:56
  • @ACatInLove you know `Dim variable=something` isn't valid VBScript syntax. Did you read the program help? – user692942 Jan 11 '18 at 18:04
  • @Lankymart I am not a programmer. I am mainly web designer with very few knowledge in ASP/Vbscrip. Regarding you and all the others programmers in stackoverflow I am nothing. Of course I do not try to compare your knowledge to mine, because I do not have that kind of knowledge at all. My problem is that my question was very clean and you blocked it. I don’t ask to break in parts the “”now”” function (like the already answered question that you say that is similar to mines. I was very specific that I was trying to break apart my string Wed, 10 Jan 2018 17:23:34 UTC. Thank you. – Freddakos Jan 11 '18 at 18:11
  • @user2986570 your knowledge of the topic is not the issue. It's your ability to search existing questions and find your answer that is. – user692942 Jan 11 '18 at 18:21
  • 2
    @Lankymart `CDate` does not recognise that string, with or without the trailing UTC. – ACatInLove Jan 11 '18 at 22:03
  • @ACatInLove even with `SetLocale` set? I’m not near a computer to test it but regardless this isn’t the first time this has been asked, they just need to put some effort in. Even a [mcve] would be enough. So far it’s just, I have this and want this...give me the code. – user692942 Jan 11 '18 at 22:17
  • @Lankymart The duplicate is WRONG answer to this specific question. To my knowledge no locales does dates backwards. Despite him talking about dates it is really just a string parsing problem. Date is a red herring. – ACatInLove Jan 11 '18 at 22:31
  • @ACatInLove in which case go and find a string parsing question and suggest that as the duplicate. Regardless it has been and will be asked again. – user692942 Jan 12 '18 at 00:00
  • 1
    I don't know how to do that. And I can't find any parsing answers that fit here. – ACatInLove Jan 12 '18 at 00:36
  • @ACatInLove Finally I have found the way to separate in different parts that UTC timestamp, with arrays as you initially proposed by you. Thank you. – Freddakos Jan 12 '18 at 13:07
  • 1
    @Lankymart I am not asking for a ready (copy paste) code. I just asked to suggest me a way. Unfortunately the CDATE that you propose cannot read the string (even without the UTC characters in the end. Next time try not naming a question as duplicate without any reason. – Freddakos Jan 12 '18 at 13:08
  • More examples of date strings - [Converting Time-Stamp String to Another Date Format in VBScript](//stackoverflow.com/q/33279052) – user692942 Jan 13 '18 at 16:52
  • @user2986570 have [updated my answer](https://stackoverflow.com/a/22575530/692942) if it helps. – user692942 Jan 13 '18 at 17:07

2 Answers2

1

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

user692942
  • 16,398
  • 7
  • 76
  • 175
  • It was completely impossible for me and my poor knowledge to manage to write so complete and professional syntax of code. I apologize for the inconvenience maybe caused by my question. – Freddakos Jan 13 '18 at 21:35
0

Use Split() to get the parts of your input string. Feed the correct parts to DateSerial()/CDate() to get a Date that should display/print as /d/m/y if that's the way of your locale/Regional settings. If you don't need a Date, build the desired String via Join(). As 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)"

output:

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)
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96