Forum Discussion

Roy's avatar
Roy
Visitor
14 years ago

How to generate an ISO-8601 string?

To work with an external API, I need to generate the current time as an ISO-8601 string. Is there an easy way to do this? roDateTime can *parse* an ISO-8601, but I don't see any way to generate one.

1 Reply

  • Function DateToISO8601String(date As Object, includeZ = True As Boolean) As String
    iso8601 = PadLeft(date.GetYear().ToStr(), "0", 4)
    iso8601 = iso8601 + "-"
    iso8601 = iso8601 + PadLeft(date.GetMonth().ToStr(), "0", 2)
    iso8601 = iso8601 + "-"
    iso8601 = iso8601 + PadLeft(date.GetDayOfMonth().ToStr(), "0", 2)
    iso8601 = iso8601 + "T"
    iso8601 = iso8601 + PadLeft(date.GetHours().ToStr(), "0", 2)
    iso8601 = iso8601 + ":"
    iso8601 = iso8601 + PadLeft(date.GetMinutes().ToStr(), "0", 2)
    iso8601 = iso8601 + ":"
    iso8601 = iso8601 + PadLeft(date.GetSeconds().ToStr(), "0", 2)
    If includeZ Then
    iso8601 = iso8601 + "Z"
    End If
    Return iso8601
    End Function

    Function PadLeft(value As String, padChar As String, totalLength As Integer) As String
    While value.Len() < totalLength
    value = padChar + value
    End While
    Return value
    End Function