formatInputToDateTime method

DateTime formatInputToDateTime({
  1. String? specificFormat,
})

Parses the string into a DateTime using a custom format.

Supported tokens:

  • yyyy → 4-digit year
  • yy → 2-digit year (interpreted with a pivot, default 50 → 1950‑2049)
  • MM → 2-digit month
  • dd → 2-digit day
  • HH → 2-digit hour (0-23)
  • mm → 2-digit minute
  • ss → 2-digit second

Supported formats out-of-the-box:

  • 'dd/MM/yyyy', 'dd/MM/yy'
  • 'dd-MM-yyyy', 'dd-MM-yy'
  • 'yyyy-MM-dd'
  • 'yyyy-MM-ddTHH:mm:ss'

Throws FormatException if the string doesn't match the expected format.

Implementation

DateTime formatInputToDateTime({String? specificFormat}) {
  final format = specificFormat ?? 'dd/MM/yyyy';
  const int pivotYear = 45;
  /// Converts a 2-digit year to 4 digits based on [pivotYear].
  /// If `yy > pivotYear` → 1900s, else → 2000s.
  ///
  /// Example:
  /// ```dart
  /// parseTwoDigitYear(30); // 2030
  /// parseTwoDigitYear(50); // 1950
  /// ```
  int parseTwoDigitYear(int yy) {
    return yy > pivotYear ? 1900 + yy : 2000 + yy;
  }

  try {
    switch (format) {
      case 'dd/MM/yyyy':
      case 'dd-MM-yyyy': {
        final sep = format.contains('/') ? '/' : '-';
        final parts = split(sep);
        if (parts.length != 3) throw FormatException('Invalid date string: $this');
        final day = int.parse(parts[0]);
        final month = int.parse(parts[1]);
        final year = int.parse(parts[2]);
        return DateTime(year, month, day);
      }
      case 'dd/MM/yy':
      case 'dd-MM-yy': {
        final sep = format.contains('/') ? '/' : '-';
        final parts = split(sep);
        if (parts.length != 3) throw FormatException('Invalid date string: $this');
        final day = int.parse(parts[0]);
        final month = int.parse(parts[1]);
        final year = parseTwoDigitYear(int.parse(parts[2]));
        return DateTime(year, month, day);
      }
      case 'yyyy-MM-dd': {
        final parts = split('-');
        if (parts.length != 3) throw FormatException('Invalid date string: $this');
        final year = int.parse(parts[0]);
        final month = int.parse(parts[1]);
        final day = int.parse(parts[2]);
        return DateTime(year, month, day);
      }
      case 'yyyy-MM-ddTHH:mm:ss': {
        final dateTimeParts = split('T');
        if (dateTimeParts.length != 2) throw FormatException('Invalid date string: $this');
        final dateParts = dateTimeParts[0].split('-');
        final timeParts = dateTimeParts[1].split(':');
        if (dateParts.length != 3 || timeParts.length != 3) throw FormatException('Invalid date string: $this');

        final year = int.parse(dateParts[0]);
        final month = int.parse(dateParts[1]);
        final day = int.parse(dateParts[2]);
        final hour = int.parse(timeParts[0]);
        final minute = int.parse(timeParts[1]);
        final second = int.parse(timeParts[2]);
        return DateTime(year, month, day, hour, minute, second);
      }
      default:
        throw UnimplementedError('Format $format not supported without intl');
    }
  } catch (e) {
    if (e is FormatException || e is UnimplementedError) rethrow;
    throw FormatException('Invalid date string: $this for format $format');
  }
}