Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter : How to Convert DateTime to TimeOfDay?

Tags:

flutter

I have this code of time picker, then I want to set the initial Time from a DateTime variable:

Future<Null> _selectTime(BuildContext context) async {
final TimeOfDay pickedS = await showTimePicker(
    context: context,
    initialTime: createdAt,
    builder: (BuildContext context, Widget child) {
       return MediaQuery(
         data: MediaQuery.of(context).copyWith(alwaysUse24HourFormat: false),
        child: child,
      );});

if (pickedS != null && pickedS != selectedTime )
  setState(() {
    selectedTime = pickedS;
    myTweets[actualTweetIndex].createdAt = dateFormat.format(selectedDate.toLocal()) +' ${selectedTime.hour}:${selectedTime.minute}:00 +0000 '+ selectedDate.year.toString();
  });

}

like image 965
AmegoDev. Avatar asked Dec 21 '25 12:12

AmegoDev.


1 Answers

The TimeOfDay class has a dedicated constructor for that:

TimeOfDay.fromDateTime(DateTime time)

You can check the documentation here

like image 144
F Perroch Avatar answered Dec 24 '25 04:12

F Perroch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!