nhuluseda
nhuluseda

Reputation: 777

How to convert string to hours in dart?

I have string like "080000000000" mean 08.00 AM. So, i'm using format DateFormat('jm');

I tried to substring 4 first string convert using DateTime.parse but throws an exception.

How to achieve the given string to 08.00 AM?

Upvotes: 1

Views: 2634

Answers (1)

anmol.majhail
anmol.majhail

Reputation: 51226

As you are only Dealing with

Time of the Day

Code :

String myVal = "080000000000";

String myHour = myVal.substring(0, 2);
String myMin = myVal.substring(2, 4);

TimeOfDay releaseTime = TimeOfDay(hour: int.parse(myHour), minute: int.parse(myMin));
print(releaseTime.format(context)); // 8:00 AM

Also for accepted string for

DateTime parse (
String formattedString
)

you can check Examples of accepted strings: - https://api.dartlang.org/stable/2.2.0/dart-core/DateTime/parse.html

For 24-hours format - add:

MaterialApp(
      builder: (context, child) =>
          MediaQuery(data: MediaQuery.of(context).copyWith(alwaysUse24HourFormat: true), child: child),

Upvotes: 5

Related Questions