tryParse method

Date tryParse(
  1. String s
)

Parses a string with format '2020-12-31'. Returns null if parsing fails.

Implementation

static Date tryParse(String s) {
  final i = s.indexOf('-');
  if (i < 0) {
    return null;
  }
  final j = s.indexOf('-', i + 1);
  if (j < 0) {
    return null;
  }
  final year = int.tryParse(s.substring(0, i));
  if (year == null) {
    return null;
  }
  final month = int.tryParse(s.substring(i + 1, j));
  if (month == null || month < 1 || month > 12) {
    return null;
  }
  final day = int.tryParse(s.substring(j + 1));
  if (day == null || day < 1 || day > 31) {
    return null;
  }
  return Date(year, month, day);
}