I added intl package and I trying to write out the screen

Text(DateFormat('dd-MM-yyyy').format('${article.date}'),),

I am getting The argument type 'String' can't be assigned to the parameter type 'DateTime' error


Solution 1: Ravindra S. Patil

Try below code hope its help to you.

For Current Time Zone

DateTime now = DateTime.now();
String formattedDate = DateFormat('dd-MMM-yyyy ').format(now);

Text(
          'Date : ' + formattedDate,
          style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
        ),

If you Use JSON format Date try below code

 String date = '';
 DateTime dateTime = DateTime.tryParse(
         article.date);
         date = DateFormat('dd-MM-yyyy').format(dateTime);

 Text(date.toString(),),


Solution 2: Josteve

The format method takes a DateTime and not a String

Assuming article.date is in an acceptable format for DateTime (Ignoring the data type).

Try this:

Text(DateFormat('dd-MM-yyyy').format(DateTime.parse('${article.date.toString()}')