【问题标题】:How to convert seconds to minutes in Dart?如何在 Dart 中将秒转换为分钟?
【发布时间】:2020-09-14 15:48:18
【问题描述】:

我正在学习 Dart 和颤振 2 天,我对如何将秒(例如 1500 秒)转换为分钟感到困惑。

为了学习新语言,我正在制作番茄计时器,出于测试目的,我想将秒转换为 MM:SS 格式。到目前为止,我在下面得到了这段代码,但我现在被困了几个小时......我用谷歌搜索但无法解决这个问题,所以我使用了 Stackoverflow。我应该如何修复代码?

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

【问题讨论】:

标签: flutter dart


【解决方案1】:

查看这段代码,了解带有格式化输出的倒数计时器

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*/

【讨论】:

    【解决方案2】:

    无格式:

    int mins = Duration(seconds: 120).inMinutes; // 2 mins
    

    格式:

    String formatTime(int seconds) {
      return '${(Duration(seconds: seconds))}'.split('.')[0].padLeft(8, '0');
    }
    
    void main() {
      String time = formatTime(3700); // 01:01:11
    }
    

    【讨论】:

    • " inMinutes "返回值可以大于 59。这个答案让我失去了 10 分钟的时间来追逐这个错误 thx
    【解决方案3】:

    这段代码对我有用

    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;
    }
    

    现在只需调用函数

    formatedTime(1500)
    

    格式化时间示例

    【讨论】:

      【解决方案4】:

      您可以使用持续时间。它是最快的方式。以下示例在 3 分钟内转换 180 秒

      Duration(seconds:180).inMinutes
      

      结果:3(分钟)

      【讨论】:

        【解决方案5】:

        这是我实现 MM:SS 所需格式的方式

        Duration(seconds: _secondsLeft--).toString().substring(2, 7);

        下面是 toString 方法返回的示例:

        d = Duration(days: 0, hours: 1, minutes: 10, microseconds: 500);
        d.toString();  // "1:10:00.000500"
        

        因此,通过链接 substring 方法,您可以轻松实现更多格式,例如HH:MM:SS 等

        【讨论】:

        • 最好的答案。
        【解决方案6】:

        你可以试试这个:

        int minutes = (seconds / 60).truncate();
        String minutesStr = (minutes % 60).toString().padLeft(2, '0');
        

        【讨论】:

        • 谢谢它现在正在工作。但为什么我需要在 String minutesStr = (minutes % 60).toString().padLeft(2, '0'); 中输入分钟?分钟现在等于 1,分钟 % 60 是 0.0166...我认为 pad left 会添加两个零...
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-01
        相关资源
        最近更新 更多