Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to convert string date in Format yyyyMMddHHmmss to DateTime dart

Tags:

flutter

dart

i have a string containing date in format yyyyMMddHHmmss (e.g.) (20180626170555) and i am using following code to convert it into date time

dateTimeFromString(json['dateTime'], "yyyyMMddHHmmss") 

exception is:

FormatException: Trying to read MM from 20180623130424 at position 14 

what can be the reason?

like image 266
Sana.91 Avatar asked Jun 26 '18 12:06

Sana.91


People also ask

How do you format the date in Dart flutter?

To format DateTime in Flutter using a standard format, you need to use the intl library and then use the named constructors from the DateFormat class. Simply write the named constructor and then call the format() method with providing the DateTime.

Can we convert string to int in Dart?

A string can be cast to an integer using the int. parse() method in Dart. The method takes a string as an argument and converts it into an integer.


1 Answers

DateTime.parse("string date here") accept some formatted string only. Check below examples of accepted strings.

  • "2012-02-27 13:27:00"
  • "2012-02-27 13:27:00.123456789z"
  • "2012-02-27 13:27:00,123456789z"
  • "20120227 13:27:00"
  • "20120227T132700"
  • "20120227"
  • "+20120227"
  • "2012-02-27T14Z"
  • "2012-02-27T14+00:00"
  • "-123450101 00:00:00 Z": in the year -12345.
  • "2002-02-27T14:00:00-0500": Same as "2002-02-27T19:00:00Z"

=> String to DateTime

DateTime tempDate = new DateFormat("yyyy-MM-dd hh:mm:ss").parse(savedDateString); 

=> DateTime to String

String date = DateFormat("yyyy-MM-dd hh:mm:ss").format(DateTime.now()); 

Reference links:

  • Use intl for DateFormat from flutter package (https://pub.dev/packages/intl)
  • DateTime.parse() => https://api.dart.dev/stable/2.7.2/dart-core/DateTime/parse.html
like image 66
Patel Pinkal Avatar answered Sep 21 '22 22:09

Patel Pinkal