Flutter - Converting minutes into H:M

戏子无情 提交于 2020-07-06 13:56:30

问题


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


回答1:


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



回答2:


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")}';
}


来源:https://stackoverflow.com/questions/56918229/flutter-converting-minutes-into-hm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!