El Hombre Sin Nombre
El Hombre Sin Nombre

Reputation: 3092

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?

Upvotes: 7

Views: 18873

Answers (4)

Matias de Andrea
Matias de Andrea

Reputation: 360

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

Upvotes: 5

The format() method receives an object of type DateTime no String. I tried to

print(DateFormat.d().format(DateTime.now())); 

and they worked correctly.I don't understand why it tells you that the d() method is undefined. Have you checked that the package matters well and that you can access the implement also from the d() method by pressing cmd + click on the method?

Upvotes: 0

Mutlu Simsek
Mutlu Simsek

Reputation: 1172

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

Upvotes: 2

mjhansen3
mjhansen3

Reputation: 993

You can simply do this:

DateFormat("EEEE").format(DateTime.now().toString())

Upvotes: 0

Related Questions