78

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?

Ahmed Wagdi
  • 3,913
  • 10
  • 50
  • 116
Sana.91
  • 1,999
  • 4
  • 33
  • 52

11 Answers11

139

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:

Patel Pinkal
  • 8,984
  • 4
  • 28
  • 50
85

intl DateFormat can't cope with your input string as it doesn't have any separators. The whole string gets consumed as the year. However DateTime.parse does cope with this (nearly). It happens to expect precisely the format you have (again, nearly).

One of the acceptable styles to parse is 20120227T132700, which just differs by the T date/time separator.

Try this:

String date = '20180626170555';
String dateWithT = date.substring(0, 8) + 'T' + date.substring(8);
DateTime dateTime = DateTime.parse(dateWithT);
Richard Heap
  • 48,344
  • 9
  • 130
  • 112
34

to convert from "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" to 'MM/dd/yyyy hh:mm a'

date = '2021-01-26T03:17:00.000000Z';
DateTime parseDate =
    new DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse(date);
var inputDate = DateTime.parse(parseDate.toString());
var outputFormat = DateFormat('MM/dd/yyyy hh:mm a');
var outputDate = outputFormat.format(inputDate);
print(outputDate)

output 01/26/2021 03:17 AM

D V Yogesh
  • 3,337
  • 1
  • 28
  • 39
24

You can use DateFormat to parse a DateTime from string to an object

// With en_US locale by default
var newDateTimeObj = new DateFormat().add_yMd().add_Hms().parse("7/10/1996 10:07:23")
// with a defined format
var newDateTimeObj2 = new DateFormat("dd/MM/yyyy HH:mm:ss").parse("10/02/2000 15:13:09")

Check the doc here.

fingerprints
  • 2,751
  • 1
  • 25
  • 45
13

The Easient way convert a string into Date format is

  print(DateTime.parse('2020-01-02')); // 2020-01-02 00:00:00.000
  print(DateTime.parse('20200102')); // 2020-01-02 00:00:00.000
  print(DateTime.parse('-12345-03-04')); // -12345-03-04 00:00:00.000
  print(DateTime.parse('2020-01-02 07')); // 2020-01-02 07:00:00.000
  print(DateTime.parse('2020-01-02T07')); // 2020-01-02 07:00:00.000
  print(DateTime.parse('2020-01-02T07:12')); // 2020-01-02 07:12:00.000
  print(DateTime.parse('2020-01-02T07:12:50')); // 2020-01-02 07:12:50.000
  print(DateTime.parse('2020-01-02T07:12:50Z')); // 2020-01-02 07:12:50.000Z
  print(DateTime.parse('2020-01-02T07:12:50+07')); // 2020-01-02 00:12:50.000Z
  print(DateTime.parse('2020-01-02T07:12:50+0700')); // 2020-01-02 00:12:50.00
  print(DateTime.parse('2020-01-02T07:12:50+07:00')); // 2020-01-02 00:12:50.00
Anand
  • 4,355
  • 2
  • 35
  • 45
7

From the docs, you need Single M to month in year :

dateTimeFromString(json['dateTime'], "yMdHms")
aaDev
  • 143
  • 6
  • The dart documentation is not very clear ! : **Number**: the minimum number of digits. Shorter numbers are zero-padded to this amount (e.g. if "m" produces "6", "mm" produces "06"). – aaDev Jun 26 '18 at 12:20
  • your solution gives: FormatException: Trying to read M from 20180623130424 at position 14 – Sana.91 Jun 26 '18 at 12:53
2

Basic information about how to convert String to Date and Date to string in flutter. Look at below link

https://quickstartflutterdart.blogspot.com/2018/10/how-to-convert-string-to-date-and-date.html

Might be it will be helped for others.

iPatel
  • 46,010
  • 16
  • 115
  • 137
0
String startdate1="10/31/2022";
String endate1="11/02/2022";


DateTime start = new DateFormat("MM/dd/yyyy").parse(startdate1); 
DateTime end = new DateFormat("MM/dd/yyyy").parse(enddate1);
DateTime s = DateTime(start.year, start.month, start.day);
DateTime to = DateTime(end.year, end.month, end.day);
int day= (to.difference(s).inHours / 24).round()+1;
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 03 '22 at 14:51
0

i did something like this (using the intl package)

final date = '7/10/1996';
final month = DateFormat.LLLL().format(DateTime.parse(date));

LLLL in the code above is date format skeleton meaning 'stand alone month', other date formatter is presented here

Darkhorse
  • 186
  • 2
  • 7
0

add String date as a parameter

DateTime.prase(String userString);
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • Please review for typos and for possible further improvement according to [answer]. An explanation of how this works and why it helps would be of benefit for the perception of your contribution. – Yunnosch Jan 02 '23 at 11:25
0

If you have a date and time string in a specific format, you can convert it to a DateTime object by using the parse() method. For example, if you have a string that contains “12/03/2019 9:45 AM”, you can use the parse() method to convert it to a DateTime object like this:

var dateTimeString = “12/03/2019 9:45 AM”; var dateTimeObject = DateTime.parse(dateTimeString);

print(dateTimeObject); // 12/03/2019 09:45:00.000 The parse() method is very versatile and can handle a variety of different formats. If your string doesn’t follow a strict format, you can use tryParse() instead. This method will return null if it fails to parse the string. for detail click here https://mycodingwork.com/flutter-convert-string-to-datetime/