Can't convert a Datetime from a string to another format (eg: 14/12/2021 03:34:03 PM to 03:34 pm)

Issue

I’am trying to convert a datetime string from one format to another. i tried to use intl package. But i don’t know how to convert this string to another format.
The datetime string i’am getting from api is 14/12/2021 03:34:03 PM. I want to show it like this in my app (only time) 03:34 pm (Also want to make PM Small letter).

Solution

Try below code hope its helpful to you. I have tried it without using any third party library

    String yourDate = '14/12/2021 03:34:03 PM';
    DateFormat formateDate = DateFormat('dd/MM/yyyy hh:mm:ss a');
    DateTime inputDate = formateDate.parse(yourDate);
    String resultDate = DateFormat('hh:mm a').format(inputDate);
    print(resultDate.toLowerCase());

Your Widget:

Text(
    'Time : ${resultDate.toLowerCase()}',
    style: TextStyle(
      fontSize: 15,
     ),
   ),

Your Result Screen-> image

Answered By – Ravindra S. Patil

Answer Checked By – Jay B. (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *