显示持续时间取决于其值。

huangapple go评论64阅读模式
英文:

How to display Duration depending on it's value

问题

以下是翻译好的部分:

例如,我有一些以下的 Duration 值:

0:12:15.000000 // 小时为 0,所以我想显示为 mm-ss => 12:15
0:01:02.000000 // 分钟小于 10,所以我想显示为 m-ss => 1:02
0:00:05.000000  // 分钟为 0,所以我想显示为 m-ss => 0:05

我从视频信息中导入这些 Duration 值,所以我不知道 .000000 的类型是什么,但我也想将它们去掉。

我应该如何最好地处理这些情况 显示持续时间取决于其值。

英文:

For example i have some of the following Duration values

0:12:15.000000 // the hour is 0 so i want to display the value like mm-ss => 12:15
0:01:02.000000 // the minutes is less than 10 so i want to display the value like m-ss => 1:02
0:00:05.000000  // the minutes is 0 so i want to the value like m-ss => 0:05

i import these Duration values from video info so i don't know what was the type of .000000 is . but i want to remove them too .

How could i handle these scenarios in best way 显示持续时间取决于其值。

答案1

得分: 1

以下是您提供的代码的中文翻译:

我猜这可能会有所帮助

String formatDuration(Duration duration) {
  String twoDigitMinutes = duration.inMinutes.remainder(60).toString().padLeft(2, '0');
  String twoDigitSeconds = duration.inSeconds.remainder(60).toString().padLeft(2, '0');
  
  if (duration.inHours == 0) {
    return '$twoDigitMinutes:$twoDigitSeconds';
  } else if (duration.inMinutes < 10) {
    return '${duration.inHours}:$twoDigitMinutes:$twoDigitSeconds';
  } else {
    return '${duration.inHours}:$twoDigitMinutes:$twoDigitSeconds';
  }
}

Duration duration = Duration(hours: 0, minutes: 12, seconds: 15);
String formattedDuration = formatDuration(duration);
print(formattedDuration); // 打印 "12:15";

duration = Duration(hours: 0, minutes: 1, seconds: 2);
formattedDuration = formatDuration(duration);
print(formattedDuration); // 打印 "1:02";

duration = Duration(hours: 0, minutes: 0, seconds: 5);
formattedDuration = formatDuration(duration);
print(formattedDuration); // 打印 "0:05";

希望这对您有所帮助。

英文:

I guess this might help

String formatDuration(Duration duration) {
  String twoDigitMinutes = duration.inMinutes.remainder(60).toString().padLeft(2, &#39;0&#39;);
  String twoDigitSeconds = duration.inSeconds.remainder(60).toString().padLeft(2, &#39;0&#39;);
  
  if (duration.inHours == 0) {
    return &#39;$twoDigitMinutes:$twoDigitSeconds&#39;;
  } else if (duration.inMinutes &lt; 10) {
    return &#39;${duration.inHours}:$twoDigitMinutes:$twoDigitSeconds&#39;;
  } else {
    return &#39;${duration.inHours}:$twoDigitMinutes:$twoDigitSeconds&#39;;
  }
}

Duration duration = Duration(hours: 0, minutes: 12, seconds: 15);
String formattedDuration = formatDuration(duration);
print(formattedDuration); // prints &quot;12:15&quot;

duration = Duration(hours: 0, minutes: 1, seconds: 2);
formattedDuration = formatDuration(duration);
print(formattedDuration); // prints &quot;1:02&quot;

duration = Duration(hours: 0, minutes: 0, seconds: 5);
formattedDuration = formatDuration(duration);
print(formattedDuration); // prints &quot;0:05&quot;

答案2

得分: -1

以下是翻译好的代码部分:

假设您有一个持续时间对象。您可以定义您的格式化函数如下并使用它。

String _formatVideoDuration(Duration duration) {
  final hours = (duration.inHours);
  final minutes = (duration.inMinutes % 60);
  final ss = (duration.inSeconds % 60).toString().padLeft(2, '0');

  if (hours > 9) {
    return '${hours.toString().padLeft(2, '0')}:${minutes.toString().padLeft(2, '0')}:$ss';
  } else if (hours > 0) {
    return '${hours.toString().padLeft(1, '0')}:${minutes.toString().padLeft(2, '0')}:$ss';
  } else if (minutes > 9) {
    return '${minutes.toString().padLeft(2, '0')}:$ss';
  } else {
    return '${minutes.toString().padLeft(1, '0')}:$ss';
  }
}
英文:

Assuming you have duration object. You can define your format function something like this and use it.

  String _formatVideoDuration(Duration duration) {
    final hours = (duration.inHours);
    final minutes = (duration.inMinutes % 60);
    final ss = (duration.inSeconds % 60).toString().padLeft(2, &#39;0&#39;);

    if (hours &gt; 9) {
      return &#39;${hours.toString().padLeft(2, &#39;0&#39;)}:${minutes.toString().padLeft(2, &#39;0&#39;)}:$ss&#39;;
    } else if (hours &gt; 0) {
      return &#39;${hours.toString().padLeft(1, &#39;0&#39;)}:${minutes.toString().padLeft(2, &#39;0&#39;)}:$ss&#39;;
    } else if (minutes &gt; 9) {
      return &#39;${minutes.toString().padLeft(2, &#39;0&#39;)}:$ss&#39;;
    } else {
      return &#39;${minutes.toString().padLeft(1, &#39;0&#39;)}:$ss&#39;;
    }
  }

huangapple
  • 本文由 发表于 2023年5月6日 15:30:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76187658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定