-1

I have a String that looks exactly like this: "12/12/2000" and I have tried to convert it into DateTime but I couldn't get what I wish it was. I want my DateTime to looks exactly like the String value. I am using Flutter. The code below is how I am trying to convert it but It doesn't show the exactly value of string:

String date = "12/12/2000";
String dateConvert = date.substring(0, 8);
DateTime dateTime = DateTime.parse(dateConvert);
print(dateTime);
Acorcdev
  • 371
  • 1
  • 3
  • 13
  • Your question is very vague. A recommendation, try using Simple Date formatter. – Tushar Banne May 28 '21 at 13:12
  • I am using Dart. – Acorcdev May 28 '21 at 13:12
  • 1
    Does this answer your question? [How do I format a date with Dart?](https://stackoverflow.com/questions/16126579/how-do-i-format-a-date-with-dart) – Tess2552 May 28 '21 at 13:12
  • @Tess2552 That question is about formatting (`DateTime` to `String`). This question is about parsing (`String` to `DateTime`). – jamesdlin May 28 '21 at 18:49
  • [How do I convert a date/time string to a DateTime object in Dart?](https://stackoverflow.com/questions/49385303/how-do-i-convert-a-date-time-string-to-a-datetime-object-in-dart) – Tess2552 May 29 '21 at 07:04

3 Answers3

2

I made it just like this:

DateTime convertedDate = new DateFormat('dd/MM/yyyy').parse("12/12/2000");
print(convertedDate);

Thank you everyone who dedicated time to answer. I hope I can help some other people!

Acorcdev
  • 371
  • 1
  • 3
  • 13
0

You can use SimpleDateFormat. Look these examples:

String sDate1="31/12/1998";  
String sDate2 = "31-Dec-1998";  
String sDate3 = "12 31, 1998";  
String sDate4 = "Thu, Dec 31 1998";  
String sDate5 = "Thu, Dec 31 1998 23:37:50";  
String sDate6 = "31-Dec-1998 23:37:50";  
SimpleDateFormat formatter1=new SimpleDateFormat("dd/MM/yyyy");  
SimpleDateFormat formatter2=new SimpleDateFormat("dd-MMM-yyyy");  
SimpleDateFormat formatter3=new SimpleDateFormat("MM dd, yyyy");  
SimpleDateFormat formatter4=new SimpleDateFormat("E, MMM dd yyyy");  
SimpleDateFormat formatter5=new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");  
SimpleDateFormat formatter6=new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");  
Date date1=formatter1.parse(sDate1);  
Date date2=formatter2.parse(sDate2);  
Date date3=formatter3.parse(sDate3);  
Date date4=formatter4.parse(sDate4);  
Date date5=formatter5.parse(sDate5);  
Date date6=formatter6.parse(sDate6);

For this you will need import:

import java.text.SimpleDateFormat;  
import java.util.Date;  
Arcaniaco
  • 400
  • 2
  • 10
0

DateTime.parse does not accept the given format, the acceptable formats are described in the documentation.

Using the intl package, you can do the following:

import 'package:intl/intl.dart';

String date = "12/12/2000";
DateTime dateTime = DateFormat("dd/MM/yyyy").parse(date);
print(dateTime);

To use the package, you have to add the dependency to your pubspec.yaml file.

I would personally recommend using the ISO 8601 date format instead, which is accepted by DateTime.parse.

Andras Timar
  • 61
  • 1
  • 3