Home > OS >  Trying to read from DATE HOUR at position 17 DateTime throwing error
Trying to read from DATE HOUR at position 17 DateTime throwing error

Time:09-22

i have two DateTimePicker's, and i wanted to set AM and PM to them this is just one of the DateTimePicker.

        DateTimePicker(
          type: DateTimePickerType.dateTimeSeparate,
          dateMask: 'd MMM, yyyy a',
          initialValue: "Enter Boarding date",
          firstDate: DateTime(2000),
          lastDate: DateTime(2100),
          icon: Icon(Icons.event),
          dateLabelText: 'Date',
          timeLabelText: "Hour",
          locale: Locale('am', 'ET'),
          use24HourFormat: false,
          onChanged: (val) {
            _boardingDate.text = val;
          },
          validator: (val) {
            print(val);
            return null;
          },
          onSaved: (val) => print(val),
        ),

  TextEditingController _boardingDate = TextEditingController();

i wanted to convert it into a value like

YEAR-MONTH-DATE HOUR:MINUTE AM/PM

and this is the way im converting it.. i dont know if im correct or not.

DateFormat inputFormat = DateFormat('yyyy-MM-dd hh:mm a');
String _arrival = inputFormat.parse(_arrivalDate.text).toString();
String _boarding = inputFormat.parse(_boardingDate.text).toString();

then it doesnt want to assign the AM/PM. when i remove the last a from the inputFormat variable it works. but when its there it throws this error

The following FormatException was thrown while handling a gesture:
Trying to read from 2021-09-25 04:20 at position 17

im using node.js as a backend to accept dateTime.

CodePudding user response:

Try this one, convert it to DataTime, then format to String

DateTime _arrivalDatetime = DateTime.parse(_arrivalDate.text);
String _arrival = DateFormat("yyyy-MM-dd hh:mm a").format(_arrivalDatetime);
DateTime _boardingDatetime = DateTime.parse(_boardingDate.text);
String _boarding = DateFormat("yyyy-MM-dd hh:mm a").format(_boardingDatetime);

Or

  String dataTimeTextFormater(String text) {
    String formatedText = '';
    DateTime datetime = DateTime.parse(text);
    formatedText = DateFormat("yyyy-MM-dd hh:mm a").format(datetime);
    return formatedText;
  }

String _arrival = dataTimeTextFormater(_arrivalDate.text);
String _boarding = dataTimeTextFormater(_boardingDate.text);

I try in enter image description here

I think you said

registers the time only

I guess you have a textfield with _boardingDate, the DateTimePicker pick the date but in wrong format, you format it but UI not update yet. you could change onchange like

onChanged: (val) {
    _boardingDate.text = dataTimeTextFormater(val);
},
  • Related