Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Can´t format Datetime

The error is simple

import 'package:intl/intl.dart';

DateFormat.d().format(DateTime.now().toString())

Return

The method 'd' isn't defined for the class 'DateFormat'. Try correcting the name to the name of an existing method, or defining a method named 'd'.

Is clear what happen but how can i fix it?

like image 934
El Hombre Sin Nombre Avatar asked Aug 03 '26 16:08

El Hombre Sin Nombre


2 Answers

Try use this to get a day of week (like at monday)

Add dependency intl

intl: ^0.16.0 // Or the last version

Code:

import 'package:intl/intl.dart';

DateFormat.EEEE().format(DateTime.now())
like image 173
Matias de Andrea Avatar answered Aug 06 '26 02:08

Matias de Andrea


You need DateFormat for only complex formatting. For this kind of simple operation, you can use the properties of the DateTime class as following:

DateTime.now().weekday

like image 31
Mutlu Simsek Avatar answered Aug 06 '26 02:08

Mutlu Simsek