Flutter:如何将DateTime转换为TimeOfDay?

时间:2020-04-23 21:17:07

标签: flutter

我有这个时间选择器代码,然后我想从DateTime变量中设置初始时间:

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();
  });

}

1 个答案:

答案 0 :(得分:1)

TimeOfDay类具有专用的构造函数:

TimeOfDay.fromDateTime(DateTime time)

您可以查看文档here