[Gambas-user] Best way converting dates into strings forth and back

Martín Belmonte mbelmonte at belmotek.net
Fri Dec 10 11:18:25 CET 2021


El 10/12/21 a las 10:38, Rolf-Werner Eilert escribió:
> would have to convert these dates into some kind of string, and back 
> into Date when loading the file(s).
> > So my question is, which way is the easiest and fastest to achieve this? 
In the case of saving a date to some storage media I suggest always 
using a text string, because dates are subject to various 
interpretations, such as the method used in shrimp, system time, 
location etc.
Then the commonly accepted date format that does not lend itself to 
confusion is ISO, specifically:
2021-12-10T11:17:01
I therefore propose two functions to retrieve and save dates:

Static Public Function TimeText(dTime As Date) As String

   Return Format(dTime, "yyyy-mm-ddThh:nn:ss")

End

Static Public Function TimeDate(sTime As String) As Date

   Dim sDate As String
   Dim aDate As Integer[]
   Dim xDate As Date
   Dim iLen As Integer

   sTime = Upper(sTime)

   If InStr(sTime, "T") > 0 Then

     Select Len(Split(sTime, "T")[1])
       Case 5   ' not seconds
         iLen = 16
       Case 8   ' include seconds
         iLen = 19
     End Select

     If InStr(sTime, "-") > 0 And InStr(sTime, ":") > 0 And Len(sTime) = 
iLen Then
       sDate = Replace(sTime, "T", "\t")
       sDate = Replace(sDate, "-", "\t")
       sDate = Replace(sDate, ":", "\t")
       aDate = Split(sDate, "\t")
       xDate = Date(aDate[0], aDate[1], aDate[2], aDate[3], aDate[4], 
aDate[5])
     Endif
   Endif

   Return xDate

End


More information about the User mailing list