Home > front end >  Convert Date in Dart Unhandled Exception: FormatException: Invalid date format
Convert Date in Dart Unhandled Exception: FormatException: Invalid date format

Time:08-05

How can I convert 28 Jul 2017 to 2017-06-28 in dart. I have tried DateTime.parse and formatter both are not working.

CodePudding user response:

Using the intl package you can do something like this:

import 'package:intl/intl.dart';

void main() {
  DateFormat dateFormatInput = DateFormat('dd MMM yyyy');
  DateTime parsedDateTime = dateFormatInput.parse('28 Jul 2017');
  print(parsedDateTime); // 2017-07-28 00:00:00.000

  DateFormat dateFormatOutput = DateFormat('yyyy-MM-dd');
  String formattedDateTime = dateFormatOutput.format(parsedDateTime);
  print(formattedDateTime); // 2017-07-28
}
  •  Tags:  
  • dart
  • Related