parseCompactDate method

  1. @override
DateTime? parseCompactDate(
  1. String? inputString
)
override

Converts the given compact date formatted string into a DateTime.

The format of the string must be a valid compact date format for the given locale. If the text doesn't represent a valid date, null will be returned.

See also:

Implementation

@override
DateTime? parseCompactDate(String? inputString) {
  if (inputString == null) {
    return null;
  }

  // Assumes US mm/dd/yyyy format
  final List<String> inputParts = inputString.split('/');
  if (inputParts.length != 3) {
    return null;
  }

  final int? year = int.tryParse(inputParts[2], radix: 10);
  if (year == null || year < 1) {
    return null;
  }

  final int? month = int.tryParse(inputParts[0], radix: 10);
  if (month == null || month < 1 || month > 12) {
    return null;
  }

  final int? day = int.tryParse(inputParts[1], radix: 10);
  if (day == null || day < 1 || day > _getDaysInMonth(year, month)) {
    return null;
  }

  try {
    return DateTime(year, month, day);
  } on ArgumentError {
    return null;
  }
}