Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Dart cannot turn a compact timestamp like 20201101153000 into a DateTime

ErrorFormatException: Invalid date format

Short answer

DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space. A 14-digit string such as 20201101153000 has no separator, so it throws FormatException. Insert a T after the eighth digit. When bad values such as month 13 must be rejected, add separators and parse strictly with DateFormat from the intl package.

  • Applies to Dart 3.x, Flutter 3.x, intl
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the exact string you pass to parse, with its length. Compare it with the accepted shapes: 20120227T132700, 2012-02-27 13:27:00.

    final s = '20201101153000';
    print('${s.length}: $s');
  2. 2Match what you see, then jump to its fix

    1. 1FormatException: Invalid date format, and the string is 14 digitsNo T or space between the date and the timeFix
    2. 2The data always uses one fixed pattern, or invalid values like month 13 must be rejectedYou want a strict, pattern-based parseFix

What does the error look like?

  • FormatException: Invalid date format

Cause 1 of 2

No T or space between the date and the time

1Check

The documented formats take the date first, then an optional time that starts with T or a space. Digits running on directly are not that shape.

try {
  DateTime.parse('20201101153000');
} on FormatException catch (e) {
  print(e);
}

2Fix

Insert the T yourself before parsing.

final s = '20201101153000';
final dt = DateTime.parse('${s.substring(0, 8)}T${s.substring(8)}');

3Verify

Print the result; it should read 2020-11-01 15:30:00.000.

print(dt);

Confirmed by api.dart.dev

Did this fix it?

Cause 2 of 2

You want a strict, pattern-based parse

1Check

DateTime.parse is not strict: the docs say it accepts out-of-range components and carries the overflow into the next unit, so "2020-01-42" becomes 2020-02-11. Strict validation is the job of DateFormat.parseStrict from the intl package.

flutter pub add intl

2Fix

Describe the pattern with DateFormat and parse with it.

import 'package:intl/intl.dart';

final s = '20201101153000';
final spaced = '${s.substring(0, 4)}-${s.substring(4, 6)}-${s.substring(6, 8)} '
    '${s.substring(8, 10)}:${s.substring(10, 12)}:${s.substring(12)}';
final dt = DateFormat('yyyy-MM-dd HH:mm:ss').parseStrict(spaced);

3Verify

Print the value, then feed it a bad month to confirm it is rejected with an exception.

print(dt);

Confirmed by pub.dev

Did this fix it?

How do I stop it coming back?

  • Store timestamps as ISO 8601 strings (with T and a zone) when you control the format.
  • Wrap parsing of external data in try/catch and decide what to show when it fails.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.