Home > OS >  DateFormat parse from Json
DateFormat parse from Json

Time:10-08

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

CodePudding user response:

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()}')

CodePudding user response:

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(),),
  • Related