0

Hi i have an array contains

 List<dynamic> am = ['09:00', '09:30', '10:00', '10:30', '11:00', '11:30'];

and if they are selected

String time = '09:00'

how can I change time to the DateTime value or TimeOfDay

Noah_J.C_Lee
  • 163
  • 2
  • 11

3 Answers3

2
import 'package:intl/intl.dart';

if your time format is fixed (for example "hours:minutes" ) you can use this method

 DateTime? _convertStringToDateTime(String time){
    DateTime? _dateTime;
    try{
      _dateTime =  DateFormat("hh:mm").parse(time);
    }catch(e){
     }
    return _dateTime;
  }
Omar Alshyokh
  • 605
  • 5
  • 7
1

You can use this:

TimeOfDay toTimeOfDay(String time){
  List<String> timeSplit = time.split(":");
  int hour = int.parse(timeSplit.first);
  int minute = int.parse(timeSplit.last);
  return TimeOfDay(hour: hour, minute: minute);
}
Josteve
  • 11,459
  • 1
  • 23
  • 35
0

https://pub.dev/packages/date_format

See this package, By means of this package you can format your DateTime.

Vishal_VE
  • 1,852
  • 1
  • 6
  • 9