toDate method

DateTime? toDate(
  1. String format, {
  2. DateTime? defaultDate,
})

Convert string to dateTime, if fail, returns defaultDate or null

Implementation

DateTime? toDate(String format, {DateTime? defaultDate}) {
  if (this == null) return defaultDate ?? null;
  try {
    return DateFormat(format).parse(this!);
  } catch (e) {
    print(e);
    return defaultDate ?? null;
  }
}