I tried converting the date by doing the below code
DateFormat('yyyy-MM-dd')
.add_yMMMd()
.parse((state as SlotRequestPopUpDataLoadedState).date)
.toString(),
);
But it throws error
Unhandled Exception: FormatException: Trying to read - from 09/01/2023 at position 3
CodePudding user response:
It looks like you are trying to parse a date string in the format "yyyy-MM-dd" using the add_yMMMd format. This is causing the FormatException because the two formats are incompatible.
To parse a date string in the "yyyy-MM-dd" format, you should use the parse method with the DateFormat("yyyy-MM-dd") formatter. Use this:
DateTime date = DateFormat("yyyy-MM-dd").parse("2023-01-09");
CodePudding user response:
You have this issue because you are trying to parse a date that contains -
but your date contains /
. Let's say this is your date:
var date = '24/01/2023';
you can convert it like this:
var Dtime = DateFormat('dd-MM-yyyy').parse(date.replaceAll('/', '-'));
print('newDate = ${DateFormat('yyyy-MM-dd').format(Dtime)}'); //newDate = 2023-01-24