UTC 时间戳格式 - 将包含 UTC 时间日期的字符串分成几部分(经典 asp/vbscript)

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

我认为我的问题被误解了:

我的字符串 (uuttcc) 包含 UTC 时间戳格式,在其他页面中创建 JAVASCRIPT。

我想再次指出,我的字符串恰好包含:2018 年 1 月 10 日,星期三 17:23:34 UTC

有什么办法可以把这个--> (Wed, 10 Jan 2018 17:23:34 UTC) 分解成那个--> (YYYY/MM/DD) ?


我有一个包含 UTC 日期/时间的字符串 (uuttcc)。

那个:

<%
Response.Write(uuttcc)
%>

给我以下结果:

2018 年 1 月 10 日,星期三 17:23:34 UTC

有什么特别的方法可以像经典 ASP/VBScript 中的 DD YY MM 一样将这个字符串分成 3 部分吗??

使用 Split() 获取输入字符串的各个部分。将正确的部分提供给 DateSerial()/CDate() 以获得 Date 如果那是你的 locale/Regional 的方式,它应该 display/print 为 /d/m/y设置。如果您不需要日期,请通过 Join() 构建所需的 String。如:

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)"

输出:

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)

本来不想回答,但评论里都是废话,请使用 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

输出:

2018/01/10

不使用 Split() 会怎样?

主要问题是让字符串被 CDate() 识别,一旦你使用 inbuilt date functions 将允许你构建任何你想要的格式。

事实上,使用 CDate() 破坏特定解析的唯一因素是 Wed,UTC,因此您可以使用 Mid()InStr()Len() 就像这个紧凑的例子一样;

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

有用的链接

  • 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