parseDateTime function

DateTime? parseDateTime(
  1. Object? date, [
  2. DateTime? def
])

Parses date as DateTime.

Can be a num (Milliseconds since Epoch).

Implementation

DateTime? parseDateTime(Object? date, [DateTime? def]) {
  if (date == null) return def;

  if (date is DateTime) {
    return date;
  }

  if (date is num) {
    if (date == 0 && def != null) return def;
    return DateTime.fromMillisecondsSinceEpoch(date.toInt());
  }

  String s;
  if (date is String) {
    s = date;
  } else {
    s = date.toString();
  }

  s = s.trim();

  if (s.isEmpty) return def;

  return DateTime.parse(s);
}