iso8601ToDate method

DateTime? iso8601ToDate()

Implementation

DateTime? iso8601ToDate() {
  try {
    // We assume that a date without a time zone component is in UTC. To handle this properly,
    // we need to set the default time zone of Joda to UTC, since by default it uses the local
    // time zone. This ensures that apps see exactly the same dates (e.g. published) no matter
    // where they are located.
    // For the same reason, the output Date will be in UTC. Apps should convert it to the local
    // time zone for display purposes, or keep it as UTC for storage.
    DateTime dateTime = DateTime.parse(this);
    if (!dateTime.isUtc) {
      return DateTime.utc(
          dateTime.year,
          dateTime.month,
          dateTime.day,
          dateTime.hour,
          dateTime.minute,
          dateTime.second,
          dateTime.millisecond,
          dateTime.microsecond);
    }
    return dateTime;
  } on Exception {
    return null;
  }
}