Android Question How to convert 2024-11-20T09:47:00 into 11/20/2024 9:47 AM?

Alex_197

Well-Known Member
Licensed User
Longtime User
Hi all.

I have a JSON string where Created field has value as 2024-11-20T09:47:00.

How to convert 2024-11-20T09:47:00 into 11/20/2024 9:47 AM?

Thank you.
 

emexes

Expert
Licensed User
How to convert 2024-11-20T09:47:00 into 11/20/2024 9:47 AM?

Given that it looks like you are converting a 24-hour time to 12-hour AM/PM time, then probably easiest to piggyback onto DateTime parsing and formatting:

B4X:
Private Sub TextField1_TextChanged (Old As String, New As String)

    Dim SaveDateFormat As String = DateTime.DateFormat

    Try
        DateTime.DateFormat = "yyyy-MM-dd'T'HH:mm:ss"    'eg 2024-11-20T09:47:00
        Dim TempTicks As Long = DateTime.DateParse(TextField1.Text)

        DateTime.DateFormat = "M/d/yyyy h:mm aa"    'eg 11/20/2024 9:47 AM
        Label1.Text = DateTime.Date(TempTicks)    'or (TempTicks + 30000) to round off to nearest minute (bonus!)
    
    Catch
        Label1.Text = "???"
    
    End Try

    DateTime.DateFormat = SaveDateFormat
        
End Sub
 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
or the hard way:

B4X:
Sub Button1_Click
    Log(jo.RunMethod("dformat",Array("2024-11-20T09:47:00", "yyyy-MM-dd'T'HH:mm:ss", "MM/dd/yyyy HH:mm:ss a")))
End Sub

#if Java
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;

    public String dformat(String dateIn, String formatIn,String formatOut) {
        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern(formatIn);
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern(formatOut);

        LocalDateTime dateTime = LocalDateTime.parse(dateIn, inputFormatter);
        return(dateTime.format(outputFormatter));
    }

#End If
 
Upvote 0
Top