parseStringToDateUtc static method

DateTime? parseStringToDateUtc(
  1. String? date, {
  2. String format = 'yyyy-MM-dd HH:mm:ss',
})

Implementation

static DateTime? parseStringToDateUtc(String? date,
    {String format = 'yyyy-MM-dd HH:mm:ss'}) {
  if (date == null || date.isEmpty) return null;

  RegExp dateRegex =
      RegExp(r'(\d{4}-\d{2}-\d{2})[T\s]+(\d{2}:\d{2}:\d{2})(.\d{1,3})?');
  Match? match = dateRegex.firstMatch(date);
  if (match == null) return null;

  date = '${match.group(1)} ${match.group(2)}';
  return DateFormat(format).parseUTC(date);
}