I am storing a date string in SharedPreferences as 2022-9-14 19:34
, but this is not recognized as DateTime when using
DateTime dt1 = DateTime.parse(_inicio);
print("fecha······· ${(dt1)}");
I am getting the following error:
Invalid date format
2022-9-14 19:34
What I need is to show the date string as 14-09-2022 19:34
EDIT
The date string is saved as follows:
var hoy = DateTime.now();
var ano = hoy.year.toString();
var mes = hoy.month.toString();
var dia = hoy.day.toString();
var turno = "";
var fechaHora = ano "-" mes "-" dia " " _timeOfDay.format(context).toString();
And _timeOfDay is the picked time from TimePicker.
CodePudding user response:
2022-09-14 19:34
is one of the valid case. So you need to add zeros where there is a single digit (month in this case)
But a better solution would be to store DateTime.now().toString()
directly in shared-preferences. You could also use a package like intl
to format the date in your choice of format.
Since you're using time as well from timepicker, you could use DateTime constructor will all 5 values like this:
new DateTime(year, month, date, hour, minute).toString();