How to show time in AM or PM format from TimeOfDay with flutter? How to show time in AM or PM format from TimeOfDay with flutter? flutter flutter

How to show time in AM or PM format from TimeOfDay with flutter?


Once you have the time selected, you can format it with new DateFormat.jm(), which will output, for example, 5:00 PM. See DateFormat docs for more.

Edit: You could do this a few ways.

One way is to use a function, like this:

String formatTimeOfDay(TimeOfDay tod) {    final now = new DateTime.now();    final dt = DateTime(now.year, now.month, now.day, tod.hour, tod.minute);    final format = DateFormat.jm();  //"6:00 AM"    return format.format(dt);}

Another way is to use this is in the example provided from the docs:

print(new DateFormat.yMMMd().format(new DateTime.now()));


When you have identified the TimeOfDay class of Flutter, you can now convert the used variable to change it's current format to 12-hour system.

TimeOfDay initialTime = TimeOfDay.now();initialTime.format(context)