isDateTime static method

String? isDateTime(
  1. String? checkValue, {
  2. String dateFormat = "dd/MM/yyyy",
  3. bool parseLoose = false,
  4. dynamic errorMessage,
})

Checks that checkValue can be turned into a datetime.

DateFormat will be used to attempt to parse checkValue. Using the format provided by dateFormat. If checkValue can't be passed then errorMessage if provided, or the default error if not. Otherwise null is returned

Implementation

static String? isDateTime(String? checkValue,
    {String dateFormat = "dd/MM/yyyy",
    bool parseLoose = false,
    errorMessage}) {
  DateFormat df = DateFormat(dateFormat);
  var parseFn = parseLoose ? df.parseLoose : df.parse;

  try {
    if (checkValue == null) {
      throw const FormatException();
    }
    parseFn(checkValue);
  } on FormatException {
    return errorMessage ?? "Field must be a valid date";
  }

  return null;
}