0

I have this formatted string date

2020/04/16 (YYYY/MM/DD)

I want to parse this string to Datetime

var string= 2020/04/16
DateTime.parse(string);

Obviusly doesn´t work, doesn´t has a date correct format.

Somebody know what is the best way to parse this string to DateTime?

El Hombre Sin Nombre
  • 2,906
  • 18
  • 49
  • 92
  • 1
    Does this answer your question? [convert datetime string to datetime object in dart?](https://stackoverflow.com/a/61394854/179715) – jamesdlin Apr 23 '20 at 18:57
  • Is corrrect in parse but when i try to convert to Date like this "DateTime.parse(DateFormat('yyyy/MM/dd').parse(string))" return The argument type 'DateTime' can't be assigned to the parameter type 'String'. – El Hombre Sin Nombre Apr 23 '20 at 19:35
  • 1
    `DateFormat.parse` already returns a `DateTime`. You don't need to parse it again. – jamesdlin Apr 23 '20 at 19:47

1 Answers1

-1

try this:

String date = '20180626170555
String dateWithT = date.substring(0, 8) + 'T' + date.substring(8);
DateTime dateTime = DateTime.parse(dateWithT);

or you try this:

savedDateString = "2012-02-27 13:27:00"
DateTime tempDate = DateFormat("yyyy-MM-dd hh:mm:ss").parse(savedDateString);
Ali Rn
  • 1,114
  • 8
  • 19