stringflutterdartdatetimegmt

How to convert String time to datetime which includes gmt


Time should be sent in this format

"2023-01-09T10:45:00.000+05:30"
  

and the String which i have to parse into datetime is only "10:00 AM" i tried doing this

 var GMTdateFormat = DateFormat('yyyy-MM-ddHH:mm:ss.000+05:30').format(
        DateTime.parse(
          (state as SlotRequestPopUpDataLoadedState)
              .slotStartTimingList
              .where((element) => element.isSelected == true)
              .map((e) => e.name)
              .toString(),
        ).toUtc(),
      );

But it is not working


Solution

  • try this:

    //input: 10:00 AM
    final time = DateFormat('hh:mm a').parse("10:00 AM");
    final year = DateTime.now().year;
    final month = DateTime.now().month;
    final day = DateTime.now().day;
    final dateString = DateFormat('$year-$month-${day}THH:mm:ss.SSS+05:30').format(time);
    print(dateString); //2023-1-9T10:00:00.000+05:30