1

How to convert String format 12/05/2020 to DateTime 2020-12-05 00:00:00.000?

Jemu Patel
  • 61
  • 2
  • 9
  • 1
    Does this answer your question? [convert datetime string to datetime object in dart?](https://stackoverflow.com/questions/49385303/convert-datetime-string-to-datetime-object-in-dart) – jamesdlin Dec 05 '20 at 08:22

3 Answers3

1

Use intl package and then:

var string = '12/05/2020';
var date1 = DateFormat('dd/MM/YYYY').parse(string);
var date2 = DateFormat('YYYY-MM-dd').format(date1);

For single line code

var date1 = DateTime.parse(DateFormat('dd/MM/yyyy').parse(string).toIso8601String());

Result

2020-05-12 00:00:00.000
SoloWolf93
  • 1,090
  • 11
  • 25
iDecode
  • 22,623
  • 19
  • 99
  • 186
0
string date1 = "12/05/2020";

DateTime dateTime = DateFormat('MM/dd/yyyy').parse(date1);

print(dateTime); // 2020-12-05 00:00:00.000
Jemu Patel
  • 61
  • 2
  • 9
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Donald Duck Dec 05 '20 at 10:14
-2

DateTime class has a method parse you can try using that. Following is an example.

String strDt = "1974-03-20 00:00:00.000";
DateTime parseDt = DateTime.parse(strDt);
print(parseDt); // 1974-03-20 00:00:00.000

Or you can use the package intl to convert it if you need more customization.

Usman Akhlaq
  • 481
  • 1
  • 3
  • 9