Android Code Snippet Convert Mysql DateTime to regular string date time

I know is not the best way, but works ans sometimes is very useful

B4X:
Sub date_mysql_to_normal(Value As String) As String
    ' value format yyyy-mm-day hh:mm:ss  -->> as mysql field
    Dim buffer() As String
    Dim datex As String
    Dim timex As String
  
    buffer = Regex.Split(" ",Value)
    datex = buffer(0)
    timex = buffer(1)
  
    Dim buffer2() As String
    buffer2 = Regex.Split("-",datex)
  
    Dim normalDate As String
  
    normalDate = buffer2(1) & "-" & buffer2(2) & "-" & buffer2(0)  & " " & timex
    Return normalDate
  
End Sub

returns : month-day-year hh:mm:ss
 
Last edited:

Mahares

Expert
Licensed User
Longtime User
but works ans sometimes is very useful
Hi Omar, you did not have to do all that work. Here is an abbreviated version of what you are trying to do.:
B4X:
Sub date_mysql_to_normal(Value As String) As String
    DateTime.DateFormat= "yyyy-MM-dd HH:mm:ss"
    Dim d As Long= DateTime.DateParse(Value)
    DateTime.DateFormat= "MM-dd-yyyy HH:mm:ss"
    Return DateTime.Date(d)
End Sub
To test it:
B4X:
Log(date_mysql_to_normal("2021-02-15 13:10:23"))   'shows: 02-15-2021 13:10:23
I kept the same name for your sub. By the way in yours, you need to add a space like this: buffer2(0) & " " & timex
 
Top