Android Question [B4X] How to convert timestamptz from a postgresql database to ticks

Alexander Stolte

Expert
Licensed User
Longtime User
I get the following date and time back from my postgresql database: 2023-08-22T14:41:15.569105992Z

how can i convert this to ticks so i can use this in B4X?
 
Solution
B4X:
Dim s As String = "2023-08-22T14:41:15.569105992Z"
    s=s.Replace("T"," ")                  
    #if B4J
        s=s.SubString2(0, s.LastIndexOf(".")+4) 
        s=$"${s}Z"$    
    #End If
    DateTime.DateFormat = "yyyy-MM-dd HH:mm:ss.SSSSSSSSS"
    Log(DateTime.DateParse(s))   '1692729675569
The last 6 digits of the string (105992) do not impact the result anyway.

Sandman

Expert
Licensed User
Longtime User
Considering that you posted this in the Android part of the forum, is it fair to assume that you can only do the conversion on the Android side? Or do you actually have the option to modify the sql that generated the result?

If it's the latter, then perhaps you can find some clue in this thread:
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Can you discard the nanoseconds?

B4X:
Dim timestamp As String = "2023-08-22T14:41:15.569105992Z"
timestamp = timestamp.Replace("T", " ").Replace("Z", "")
timestamp = timestamp.SubString2(0, 23)
Log(timestamp)
DateTime.DateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
Dim value As Long = DateTime.DateParse(timestamp)
Log(value)
Log(DateTime.Date(value))
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Considering that you posted this in the Android part of the forum, is it fair to assume that you can only do the conversion on the Android side? Or do you actually have the option to modify the sql that generated the result?
I edited the title to be more specific, thanks.
Can you discard the nanoseconds?
I would like to leave everything by default. I am currently working on a Supabase class and this is what the API gives me back by default.
I'll try, thanks
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I don't think that format is limited to Postgre. It is common. The T is a place holder for a space:
B4X:
Dim s As String = "2023-08-22T14:41:15.569105992Z"
    s=s.Replace("T"," ")  
    DateTime.DateFormat = "yyyy-MM-dd HH:mm:ss.SSSSSSSSS"
    Log(DateTime.DateParse(s))   '1692729675569
 
Upvote 1

aeric

Expert
Licensed User
Longtime User
I don't think that format is limited to Postgre. It is common. The T is a place holder for a space:
B4X:
Dim s As String = "2023-08-22T14:41:15.569105992Z"
    s=s.Replace("T"," ")  
    DateTime.DateFormat = "yyyy-MM-dd HH:mm:ss.SSSSSSSSS"
    Log(DateTime.DateParse(s))   '1692729675569
It works in B4A but not in B4J.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
B4X:
Dim s As String = "2023-08-22T14:41:15.569105992Z"
    s=s.Replace("T"," ")                  
    #if B4J
        s=s.SubString2(0, s.LastIndexOf(".")+4) 
        s=$"${s}Z"$    
    #End If
    DateTime.DateFormat = "yyyy-MM-dd HH:mm:ss.SSSSSSSSS"
    Log(DateTime.DateParse(s))   '1692729675569
The last 6 digits of the string (105992) do not impact the result anyway.
 
Upvote 1
Solution
Top