How to convert String to TimeOfDay in flutter?

2020-08-17 06:27发布

I have a String, lets say '16:00' and I want to convert it to a TimeOfDay type. I checked the documentation and it only provides a conversor from DateTime to TimeOfDay, but I cant find a way to convert from String to TimeOfDay.

标签: dart flutter
3条回答
祖国的老花朵
2楼-- · 2020-08-17 07:01

like this:

TimeOfDay stringToTimeOfDay(String tod) {
  final format = DateFormat.jm(); //"6:00 AM"
  return TimeOfDay.fromDateTime(format.parse(tod));
}
查看更多
虎瘦雄心在
3楼-- · 2020-08-17 07:04

Try this

TimeOfDay _startTime = TimeOfDay(hour:int.parse(s.split(":")[0]),minute: int.parse(s.split(":")[1]));

This Worked for me..

查看更多
别忘想泡老子
4楼-- · 2020-08-17 07:08

You can do something like this:

TimeOfDay time = TimeOfDay(hour: s.split(":")[0], minute: s.split(":")[1]);

s is the string you wanna convert.

查看更多
登录 后发表回答