B4J Question Datetime to Timestamp

darabon

Active Member
I have a UTC datetime as follows:
2021-01-17 10:42:28

How do I can convert this datetime to my timezone?
I try to parse it with DateTimeParse and set timezone to my needing timezone and then get time.
But it's wrong
Please help me
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Best way is to add the timezone to the string:
B4X:
Sub AppStart (Args() As String)
    Dim t As Long = DateToTicks("2021-01-17 10:42:28")
    Log($"$DateTime{t}"$)
End Sub

Sub DateToTicks (s As String) As Long
    Dim t As String = DateTime.DateFormat
    DateTime.DateFormat = "yyyy-MM-dd HH:mm:ss Z"
    Dim res As Long = DateTime.DateParse(s & " +0000")
    DateTime.DateFormat = t
    Return res
End Sub
 
Upvote 0

darabon

Active Member
Best way is to add the timezone to the string:
B4X:
Sub AppStart (Args() As String)
    Dim t As Long = DateToTicks("2021-01-17 10:42:28")
    Log($"$DateTime{t}"$)
End Sub

Sub DateToTicks (s As String) As Long
    Dim t As String = DateTime.DateFormat
    DateTime.DateFormat = "yyyy-MM-dd HH:mm:ss Z"
    Dim res As Long = DateTime.DateParse(s & " +0000")
    DateTime.DateFormat = t
    Return res
End Sub
Thank you but your code have a problem
We have to set timezone to 0 (UTC)
and then DateTimeParse and again set timezone to us country and again get time
In this case, We get local time according to UTC time
Isn't it Erel?
 
Upvote 0
Top