parse static method

Date parse(
  1. String dateStr, {
  2. String? format,
  3. bool utc = false,
})

Constructs a new DateTime instance based on dateStr.

Given user input, attempt to parse the dateStr into the anticipated format, treating it as being in the local timezone.

If dateStr does not match our format, throws a FormatException. This will accept dates whose values are not strictly valid, or strings with additional characters (including whitespace) after a valid date. For stricter parsing, use dateStr.

Implementation

static Date parse(
  String dateStr, {
  String? format,
  bool utc = false,
}) {
  if (format == null || format == '') {
    return DateTime.parse(dateStr).date;
  }

  final formatter = DateFormat(format);
  final dateTimeFromStr = formatter.parse(dateStr, utc);
  return dateTimeFromStr.date;
}