8

In VBScript, does FormatDateTime have ISO 8601 support?

If not, how would I write such function with it?

For example:

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

Function FormatAsISO8601(datetime)
    ...
End Function
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Alon Gubkin
  • 56,458
  • 54
  • 195
  • 288

4 Answers4

13

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
Sebastian
  • 6,293
  • 6
  • 34
  • 47
Alon Gubkin
  • 56,458
  • 54
  • 195
  • 288
4

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
rheitzman
  • 2,247
  • 3
  • 20
  • 36
0

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

Here's a VBScript class that someone wrote.

Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • 1
    Link is no more actual. – Bohdan Kuts Nov 20 '18 at 10:55
  • You just use the [built in date/time functions](https://stackoverflow.com/a/6900298/692942) to format the date time as you see fit. – user692942 Jul 18 '21 at 11:24
  • Hi @user692942, the question (from a decade ago) was whether VBScript, specifically the `FormatDateTime` function, has support for ISO-8601, which, at that time (and still AFAIK) was **no**. Yes, you can manually creating one using Seconds, Minutes, Hours, etc., but that's not built in. By that same logic, you could say "Yes, VBScripts supports bittorrent, just use a raw TCP/UDP stream and write your own logic." – Chris Haas Jul 19 '21 at 18:08
  • @ChrisHaas I take your point, but due to VBScript binary support, you'd have to use a COM component to write a TCP/UDP stream whereas date functions have been there since its inception. – user692942 Jul 19 '21 at 18:21
  • You are correct, and they've been there without ISO-8601 support! ;) We're splitting hairs at a certain point, and I'm trying to cull memories from a very long time ago, like 4 Guys From Rolla era! But there's native or pure VBScript, and there's what everyone really used. Pure VBScript couldn't even talk to a database or the file system, you were forced to used COM to do pretty much anything of consequence short of local helper scripts, of which I also wrote plenty. Ah, good memories. I don't miss it, but it was the first language I ever used professionally, so there's some nostalgia. – Chris Haas Jul 19 '21 at 18:37
-1

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