Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert time to am/pm flutter

How to convert the time to am/pm ?

I have this output

I/flutter (17720): 9:00:00
I/flutter (17720): 11:00:00
I/flutter (17720): 12:00:00

This is what I have tried

 final item = snapshot.data[index];
 print("Time " + item['time'].toString());

 DateTime dateTime = DateTime.parse(item['time'].toString());
 print(DateUtil().formattedTime(dateTime));

DateUtil

 String formattedTime(DateTime dateTime) {
    return DateFormat().add_jm().format(dateTime);
  }

Error

I/flutter (17720): Time 09:00:00

════════ Exception caught by widgets library ═══════════════════════════════════ The following FormatException was thrown building Tags(dirty, state: TagsState#b3a2f): Invalid date format 09:00:00

like image 919
AI. Avatar asked Mar 19 '20 08:03

AI.


1 Answers

Use this code:

DateFormat('hh:mm a').format(DateTime.now());

According to the intl library, it states that a represents AM/PM.

like image 161
user14761376 Avatar answered Oct 11 '22 21:10

user14761376