Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBScript ISO8601

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
like image 559
Alon Gubkin Avatar asked Aug 01 '11 14:08

Alon Gubkin


People also ask

How do I use ISO 8601?

ISO 8601 represents date and time by starting with the year, followed by the month, the day, the hour, the minutes, seconds and milliseconds. For example, 2020-07-10 15:00:00.000, represents the 10th of July 2020 at 3 p.m. (in local time as there is no time zone offset specified—more on that below).

How do I get the current date in vbscript?

How-to: Get the current date [getdate. vbs] Return the current Year/Month/Day and Time formatted as a string. GetDate.

Who uses ISO 8601?

ISO 8601 can be used by anyone who wants to use a standardized way of presenting: Date. Time of day. Coordinated Universal Time (UTC)


2 Answers

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
like image 65
Alon Gubkin Avatar answered Sep 18 '22 14:09

Alon Gubkin


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
like image 44
rheitzman Avatar answered Sep 20 '22 14:09

rheitzman