开发者

VBScript ISO8601

开发者 https://www.devze.com 2023-03-24 02:46 出处:网络
In VBScript, does FormatDateTime have ISO 8601 support? If not, how would I write such function with i开发者_如何转开发t?

In VBScript, does FormatDateTime have ISO 8601 support?

If not, how would I write such function with i开发者_如何转开发t?

For example:

Response.Write FormatAsISO8601(#05/04/2011#)

Function FormatAsISO8601(datetime)
    ...
End Function


Here is the specific code I needed from Chris' class, a bit more optimized:

Public Function ToIsoDateTime(datetime) 
    ToIsoDateTime = ToIsoDate(datetime) & "T" & ToIsoTime(datetime) & CurrentTimezone
End Function

Public Function ToIsoDate(datetime)
    ToIsoDate = CStr(Year(datetime)) & "-" & StrN2(Month(datetime)) & "-" & StrN2(Day(datetime))
End Function    

Public Function ToIsoTime(datetime) 
    ToIsoTime = StrN2(Hour(datetime)) & ":" & StrN2(Minute(datetime)) & ":" & StrN2(Second(datetime))
End Function

Private Function StrN2(n)
    If Len(CStr(n)) < 2 Then StrN2 = "0" & n Else StrN2 = n
End Function


Here's a brute force function:

sDate = iso8601Date(Now)
msgbox sDate

Function iso8601Date(dt)
    s = datepart("yyyy",dt)
    s = s & RIGHT("0" & datepart("m",dt),2)
    s = s & RIGHT("0" & datepart("d",dt),2)
    s = s & "T"
    s = s & RIGHT("0" & datepart("h",dt),2)
    s = s & RIGHT("0" & datepart("n",dt),2)
    s = s & RIGHT("0" & datepart("s",dt),2)
    iso8601Date = s
End Function


Not without loading some COM component as far as I know.

Here's a VBScript class that someone wrote.


Some corrections

Function iso8601Date(dt)
    s = datepart("yyyy",dt)
    s = s & "-" & RIGHT("0" & datepart("m",dt),2)
    s = s & "-" & RIGHT("0" & datepart("d",dt),2)
    s = s & "T"
    s = s & RIGHT("0" & datepart("h",dt),2)
    s = s & ":" & RIGHT("0" & datepart("n",dt),2)
    s = s & ":" & RIGHT("0" & datepart("s",dt),2)
    iso8601Date = s
End Function
0

精彩评论

暂无评论...
验证码 换一张
取 消