formatDateString method

String formatDateString(
  1. String input
)

Implementation

String formatDateString(String input) {
  // Regular expression to match date-time formats
  RegExp dateTimeRegex =
      RegExp(r'\d{4}-\d{2}-\d{2}(\s|T)?(\d{2}:\d{2}(:\d{2})?)?(\.\d+)?Z?');

  if (dateTimeRegex.hasMatch(input)) {
    final date = DateTime.tryParse(input);

    if (date == null) return input;

    return DateFormat('yyyy-MM-dd HH:mm:ss').format(date);
  }

  // Return input as-is if it's not a date-time string
  return input;
}