# FormatException: Unexpected character (at character N) while parsing date string

- **ID:** `flutter/dart-format-exception`
- **Domain:** flutter
- **Category:** data_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

A date string parsed by intl or another package does not match the expected format pattern, often due to locale differences or malformed input.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Flutter 3.13.0 | active | — | — |
| Dart 3.1.0 | active | — | — |
| intl 0.18.1 | active | — | — |

## Workarounds

1. **Validate and sanitize the date string before parsing. Example: String cleaned = dateString.replaceAll(RegExp(r'[^0-9:/\- ]'), ''); DateTime parsed = DateFormat('yyyy-MM-dd HH:mm:ss').parse(cleaned);** (85% success)
   ```
   Validate and sanitize the date string before parsing. Example: String cleaned = dateString.replaceAll(RegExp(r'[^0-9:/\- ]'), ''); DateTime parsed = DateFormat('yyyy-MM-dd HH:mm:ss').parse(cleaned);
   ```
2. **Use a flexible parser like DateTime.tryParse() or the intl package's DateFormat with multiple patterns. Example: DateTime? date = DateFormat('yyyy-MM-dd').tryParse(input) ?? DateFormat('MM/dd/yyyy').tryParse(input);** (90% success)
   ```
   Use a flexible parser like DateTime.tryParse() or the intl package's DateFormat with multiple patterns. Example: DateTime? date = DateFormat('yyyy-MM-dd').tryParse(input) ?? DateFormat('MM/dd/yyyy').tryParse(input);
   ```
3. **Log the exact input string and expected format to debug the mismatch. Add a debug print: print('Parsing date: $input with format $pattern');** (95% success)
   ```
   Log the exact input string and expected format to debug the mismatch. Add a debug print: print('Parsing date: $input with format $pattern');
   ```

## Dead Ends

- **Change the date format pattern randomly until it works** — Without understanding the input format, random changes are unlikely to match the actual string. (90% fail)
- **Use try-catch to swallow the exception** — Silencing the error does not fix the underlying data issue and may lead to silent failures. (80% fail)
