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

Question

Asked by MURALI K on December 14, 2021 (source).

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).

Answer

Question answered by Ravindra S (source).

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

DART DATE DATETIME FLUTTER FLUTTER-WEB
SHARE: