5

I'm learning Dart & flutter for 2 days and I'm confused about how to convert seconds (for example 1500sec) to minutes.

For studying the new language, I'm making Pomodoro timer, and for test purposes, I want to convert seconds to MM: SS format. So far, I got this code below, but I'm stuck for a couple of hours now... I googled it but could not solve this problem, so I used Stackoverflow. How should I fix the code?

int timeLeftInSec = 1500;
void startOrStop() {
  timer = Timer.periodic(Duration(seconds: 1), (timer) {
    setState(() {
      if (timeLeftInSec > 0) {
        timeLeftInSec--;
        timeLeft = Duration(minutes: ???, seconds: ???)
      } else {
        timer.cancel();
      }
    }
  }
}

5 Answers5

16

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
}
CopsOnRoad
  • 175,842
  • 51
  • 533
  • 380
  • inMinutes "The returned value can be greater than 59." this answer made me lose 10 minutes chasing the bug thx – cs guy Nov 02 '20 at 15:22
10

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

enter image description here

Akash Lilhare
  • 215
  • 4
  • 10
6

You can try this :

int minutes = (seconds / 60).truncate();
String minutesStr = (minutes % 60).toString().padLeft(2, '0');
Augustin R
  • 6,023
  • 2
  • 21
  • 48
griffins
  • 5,390
  • 3
  • 23
  • 46
  • Thank you it's working now. but why I need to put minutes in String minutesStr = (minutes % 60).toString().padLeft(2, '0');? Minutes is now equal to 1 and minutes % 60 is .0166... I think pad left is gonna add two zeros... –  May 28 '20 at 00:09
3

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.

Nikage
  • 548
  • 2
  • 6
  • 18
0

Check out this piece of code for a count down timer with formatted output

import 'dart:async';

void main(){
  late Timer timer;
  int startSeconds = 120; //time limit
  String timeToShow = "";
  timer =  Timer.periodic(Duration(seconds:1 ),(time){
    startSeconds = startSeconds-1;
    if(startSeconds ==0){
      timer.cancel();
     }

    int minutes = (startSeconds/60).toInt();
    int seconds = (startSeconds%60);
   timeToShow = minutes.toString().padLeft(2,"0")+"."+seconds.toString().padLeft(2,"0");
   print(timeToShow);
  });
}

    /*output
    01.59
    01.58 
    01.57
    ...
    ...
    ...
    00.02
    00.01
    00.00*/