alternative4
alternative4

Reputation: 383

FormatException: Invalid date format 2018-08-1

If I write, in DartPad, print(DateTime.parse("2012-12-12")); it works. So why doesn't it work when I do the following (it reports an "invalid date format"):

  for (int i = 1; i <= 28; i = i + 7) {
    _activities[DateTime.parse('2012-12-$i')] = activityNames.toList();
  }

Upvotes: 9

Views: 37304

Answers (2)

Aldanis Vigo
Aldanis Vigo

Reputation: 189

Because DateTime.parse, expects 4 digits for Year, 2 digits for month, and 2 digits for day. In your for loop i begins as 1 which is a number below 9 so since it's a single digit number he parse function is not receiving the expected input. Hence, the compilation issue.I know this is mad late but I hope this helps somebody.

Upvotes: 12

Amit Prajapati
Amit Prajapati

Reputation: 14315

Add this to your package’s pubspec.yaml file:

https://pub.dev/packages/intl#-readme-tab-

dependencies:

intl: ^0.16.0

 var now = new DateTime.now();
 print(new DateFormat("yyyy-MM-dd").format(now));

For more should go for this :https://androidkt.com/format-datetime-in-flutter/

Upvotes: 16

Related Questions