Search code examples
flutterdartdatetimepickertimepicker

Flutter TimePicker to RFC 3339 format


I am using Flutters TimePicker to allow user to select a time.

This uses the TimeOfDay Class.

I need to the selection in RFC 3339 format (2022-06-24T01:23:45)

Is there a way to convert it or do I need another package to select instead?

I know that there is a date parameter in there too and the current day would be what I want to insert, not sure of this can be auto populated with current date?

When I try to convert it with a function I receive

The argument type 'TimeOfDay' can't be assigned to the parameter type 'DateTime'.
  Future<void> _selectTime(BuildContext context) async {
  final TimeOfDay? timeOfDay = await showTimePicker(
    context: context,
    builder: (BuildContext context, Widget ?child) {
      return Theme(
        data: ThemeData(
          primarySwatch: Colors.grey,
          splashColor: Colors.white,
          textTheme: TextTheme(
            subtitle1: TextStyle(color: Colors.black),
            button: TextStyle(color: Colors.black),
          ),
          colorScheme: ColorScheme.light(
              primary: Color(0xff2F2F2F),
        ),
        child: child ??Text(""),
      );
    },
    
    initialTime: selectedTime,
    initialEntryMode: TimePickerEntryMode.dial,
  );
      if(timeOfDay != null && timeOfDay != selectedTime)
        {
          setState(() {
            selectedTime = timeOfDay;
            this._myDepTime = selectedTime.format(context);
          });
      } 
  }

Is a conversion to RFC 3339 format possible or should I switch TimePicker package to a DateTime style alternative?

Thank you


Solution

  • Solution was

                final now = new DateTime.now();
                final selectedTime = new DateTime(now.year, now.month, now.day, timeOfDay.hour, timeOfDay.minute
    

    This uses DateTimes now parameter to fill the date part