How to convert seconds to minutes in Dart?
Without formatting:
int mins = Duration(seconds: 120).inMinutes; // 2 mins
Formatting:
String formatTime(int seconds) {
return '${(Duration(seconds: seconds))}'.split('.')[0].padLeft(8, '0');
}
void main() {
String time = formatTime(3700); // 01:01:11
}
this code works for me
String formatedTime(int secTime) {
String getParsedTime(String time) {
if (time.length <= 1) return "0$time";
return time;
}
int min = secTime ~/ 60;
int sec = secTime % 60;
String parsedTime =
getParsedTime(min.toString()) + " : " + getParsedTime(sec.toString());
return parsedTime;
}
now just call the function
formatedTime(1500)
formated time example
You can try this :
int minutes = (seconds / 60).truncate();
String minutesStr = (minutes % 60).toString().padLeft(2, '0');
This is the way I've achieved desired format of MM:SS
Duration(seconds: _secondsLeft--).toString().substring(2, 7);
Here is an example of what toString method returns:
d = Duration(days: 0, hours: 1, minutes: 10, microseconds: 500);
d.toString(); // "1:10:00.000500"
So with the substring
method chained you can easily achive more formats e.g. HH:MM:SS etc.