I got data input in TextField
using showDatePicker
. I converted the selected data to String and parsed it to the format dd-mm-yy
using intl format. According to docs DateFormat('ymd')
is the way to do that. But I get the error FormatException: Trying to read m from 2022-06-04 00:00:00.000 at position 4
. What is right way to convert to that format and why am I getting that error.
docs that I referred - https://api.flutter.dev/flutter/intl/DateFormat-class.html
code
TextField(
keyboardType: TextInputType.datetime,
controller: paidDateController,
onTap: () async {
DateTime? PickedDate = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2010),
lastDate: DateTime(2026));
if (PickedDate != null) {
setState(() {
formatDate = PickedDate.toString();
paidDate = DateFormat('ymd').parse(formatDate);
print(paidDate);
});
} else {
Get.snackbar('Error', 'Pick paid date',
backgroundColor: Colors.red);
}
},
decoration:
kTextFieldDecocation.copyWith(hintText: 'Paid Date'),
),
formatDate
is string data type and paidDate
is date time data type.
hmmmm ithink the formating you giving to this part
show always error since if you tried to parse the date was format given by showDateTimePicker would be like
2022-06-05 15:00:12
which also if you try to pass this. This would like likely not to parse sinceDateFormat('yMd')
orDateFormat.yMd()
which the reading pattern date of this is like this"05/06/2022"
so you try to is like thisbut if you wanted a result like 5/6/2022 try this