-1

I tried calling this code:

var test = int.parse(DateFormat('yyyy').format(formattedTemporaryDate));

but I get the error:

type 'String' is not a subtype of type 'DateTime'

my formattedTemporaryDate is '2022-01-08'

I think I need to transform the formattedTemporaryDate to an int, but I don't know how. And I even don't know if this really is the problem... Do you need more information to fix my problem?

SOS video
  • 436
  • 9
  • 21

2 Answers2

1

Just use DateTime.parse('2022-01-08') instead of just String.

Something like var formattedTemporaryDate = DateTime.parse('2022-01-08');

zabaykal
  • 1,134
  • 1
  • 8
  • 21
1

What would you like var test to be at the end? A datetime? An int representation of the date?

You can convert your string to a DateTime object by calling DateTime.parse()

  var formattedTemporaryDate = '2022-01-08';
  DateTime dt = DateTime.parse(formattedTemporaryDate);
  print(dt); // 2022-01-08 00:00:00.000

If you then want to convert it to an Int (maybe because you're doing UNIX Timestamp stuff?) You could try reverse-engineering some of the code people added here going the other direction.