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.