datetimeFormatter method

String datetimeFormatter({
  1. String outputFormat = "dd/MM/yyyy",
  2. String? inputFormat,
})

Implementation

String datetimeFormatter({String outputFormat = "dd/MM/yyyy", String? inputFormat}) {
  try {
    DateTime? dt = DateTime.tryParse(this);
    if (dt != null) {
      return DateFormat(outputFormat).format(dt);
    }
    if (inputFormat != null) {
      dt = DateFormat(inputFormat).tryParse(this);
      if (dt != null) {
        return DateFormat(outputFormat).format(dt);
      }
    } else {
      return "Unable to convert the given string into your following datetime. Please provide your input date format.";
    }

    return "Unable to convert the given string into your following datetime";
  } catch (e) {
    return "Error formatting date: $e";
  }
}