asDateTimeOrNull method

DateTime? asDateTimeOrNull({
  1. PickDateFormat? format,
})

Parses the picked value as ISO 8601 String to DateTime or returns null

Tries to parse the most common date formats such as ISO 8601, RFC 3339, RFC 1123, RFC 5322 and ANSI C's asctime()

Optionally accepts a format defining the exact to be parsed format. By default, all formats will be attempted

Examples of parsable date formats:

  • '2012-02-27 13:27:00'
  • '2012-02-27 13:27:00.123456z'
  • '2012-02-27 13:27:00,123456z'
  • '20120227 13:27:00'
  • '20120227T132700'
  • '20120227'
  • '+20120227'
  • '2012-02-27T14Z'
  • '2012-02-27T14+00:00'
  • '-123450101 00:00:00 Z': in the year -12345.
  • '2002-02-27T14:00:00-0500': Same as '2002-02-27T19:00:00Z'
  • 'Thu, 1 Jan 1970 00:00:00 GMT'
  • 'Thursday, 1-Jan-1970 00:00:00 GMT'
  • 'Thu Jan 1 00:00:00 1970'

Implementation

DateTime? asDateTimeOrNull({PickDateFormat? format}) {
  if (value == null) return null;
  try {
    return _parse(format: format);
  } catch (_) {
    return null;
  }
}