Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Converting minutes into H:M

I'm looking for a method to convert minutes into hours and minutes. I'm using the intl package through DateFormat but this requires both hours and minutes so it won't do.

If I have 100 minutes, I would like this to be converted to 01:40. Thanks

like image 366
Jake Avatar asked Jul 06 '19 23:07

Jake


2 Answers

Does this work?

String durationToString(int minutes) {
    var d = Duration(minutes:minutes);
    List<String> parts = d.toString().split(':');
    return '${parts[0].padLeft(2, '0')}:${parts[1].padLeft(2, '0')}';
}

print(durationToString(100)); //returns 01:40
like image 188
socasanta Avatar answered Sep 28 '22 23:09

socasanta


This will work for you


String getTimeString(int value) {
  final int hour = value ~/ 60;
  final int minutes = value % 60;
  return '${hour.toString().padLeft(2, "0")}:${minutes.toString().padLeft(2, "0")}';
}
like image 32
Ajil O. Avatar answered Sep 29 '22 01:09

Ajil O.