parseDateTime static method

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

Tries to parse a DateTime.

  • Returns def if value is invalid.

Implementation

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

  if (value is DateTime) {
    return value;
  } else if (value is int) {
    return DateTime.fromMillisecondsSinceEpoch(value);
  } else {
    var s = '$value'.trim();
    var ms = int.tryParse(s);
    if (ms != null) {
      return DateTime.fromMillisecondsSinceEpoch(ms);
    }
    return DateTime.tryParse(s) ?? def;
  }
}