Android Code Snippet ISO 8601 date/time formatting

Subname: Not a sub, just a snippet of code.

Description: A code snippet to convert the current date/time into an ISO 8601 format string.
B4X:
Dim dt As AHDateTime
dt.Initialize
dt.Pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
Dim ISO8601 As String
ISO8601 = dt.Format(DateTime.Now)
Dependencies: AHLocale

Tags: ISO8601, ISO 8601

Thanks to Corwin42 for his excellent libraries.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Thank you for sharing. BUT: this is the Code-Snippetforum. Please follow the instructions here to fit the thread. You have forgotten to use the needed Tags, Description and references
 

pjetson

Member
Licensed User
Longtime User
Thanks, Manfred. I hope I've edited the posting into something more useful.
 

DonManfred

Expert
Licensed User
Longtime User
Thanks, Manfred. I hope I've edited the posting into something more useful.
yes. That should be enough. Thank you for changing

BTW; It´s not really needed to use AHLocale-lib. You can just write a small sub to manage this... I´ll write some this evening if needed but now i have to go to work...
 

pjetson

Member
Licensed User
Longtime User
I need the date/time to be displayed in the format for my own locale, but the date/time POSTed to the server needs to be in ISO 8601 format. In my reading of various posts here, using AHlocale for the server format seemed the best way to do it.

Of course, I'm just a beginner here!
 

DonManfred

Expert
Licensed User
Longtime User
You can use this sub. It does not need any referenced library.

B4X:
Sub GetISO8601(dt As Long) As String
    Dim res As String ' dim string for the result
    Dim rememberdtformat As String = DateTime.DateFormat ' Get actual DateFormat-string
    Dim remembertmformat As String = DateTime.TimeFormat ' Get actual TimeFormat-string
    'DateTime.DateFormat = "yyyy-MM-ddTHH:mm:ss.SSSZ"
    DateTime.DateFormat = "yyyy-MM-dd" ' define the date-part...
    DateTime.TimeFormat = "HH:mm:ss.SSS" ' define the time-part...
    res = DateTime.Date(dt)&"T"&DateTime.Time(dt)&"Z" ' Combine all together to return it as ISO8601
    DateTime.DateFormat = rememberdtformat ' Set back DateFormat to it´s default
    DateTime.TimeFormat = remembertmformat ' Set back TimeFormat to it´s default
    Return res ' return the string build...
End Sub

Testcode
B4X:
Log(GetISO8601(DateTime.Now))
2014-10-06T09:34:26.745Z
 

pjetson

Member
Licensed User
Longtime User
I'm not sure that your ISO 8601 format is correct, Manfred.

I had to do quite a bit of reading about ISO 8601 to properly understand it, and the Z at the end means that the time is in UTC (which will only be true if you're in England), and the regional settings on your device are set correctly. If you're anywhere else, either the local date/time has to be adjusted to the equivalent date/time at the UTC meridian, or the offset from UTC has to appear at the end instead of the Z. In my case, the offset is currently +1100 since Australia (where I am) just changed to daylight saving time. It appears that you're in Germany, so your offset is +0200.

I originally thought that I would have to calculate and add or subtract the offset from local time to UTC to get ISO 8601 format, and I didn't like that idea - too much code to cater for every circumstance; moving the time back or forth up to 12 hours could result in a different day, or month, or even year!

Then I realised that ISO 8601 allows local time as long as you include the proper offset from UTC (instead of the Z). Luckily, the AHlocale library format string includes the offset, and even adjusts it for daylight savings.
 
Last edited:

Mark Read

Well-Known Member
Licensed User
Longtime User
You can use this sub. It does not need any referenced library.

B4X:
Sub GetISO8601(dt As Long) As String
    Dim res As String ' dim string for the result
    Dim rememberdtformat As String = DateTime.DateFormat ' Get actual DateFormat-string
    Dim remembertmformat As String = DateTime.TimeFormat ' Get actual TimeFormat-string
    'DateTime.DateFormat = "yyyy-MM-ddTHH:mm:ss.SSSZ"
    DateTime.DateFormat = "yyyy-MM-dd" ' define the date-part...
    DateTime.TimeFormat = "HH:mm:ss.SSS" ' define the time-part...
    res = DateTime.Date(dt)&"T"&DateTime.Time(dt)&"Z" ' Combine all together to return it as ISO8601
    DateTime.DateFormat = rememberdtformat ' Set back DateFormat to it´s default
    DateTime.TimeFormat = remembertmformat ' Set back TimeFormat to it´s default
    Return res ' return the string build...
End Sub

Testcode
B4X:
Log(GetISO8601(DateTime.Now))

@manfred

I was just wondering how to convert a gps time to this format and hey presto, someone has done it already. Now I can continue with my saving of gpx file.

Many thanks for your code.
 
Top