format12hourTime function
Implementation
String format12hourTime(String time, {String format = 'hh:mm a'}) {
// Check if the input is a valid time string in the format HH:mm:ss
if (RegExp(r'^\d{2}:\d{2}:\d{2}$').hasMatch(time)) {
// Parse the time manually
List<String> timeParts = time.split(':');
int hours = int.parse(timeParts[0]);
int minutes = int.parse(timeParts[1]);
// Create a DateTime object with today's date and the parsed time
DateTime now = DateTime.now();
DateTime dateTime = DateTime(now.year, now.month, now.day, hours, minutes);
// Format the DateTime object to the desired time format
return DateFormat(format).format(dateTime);
} else {
throw FormatException("Invalid time format");
}
}