Dart 将持续时间格式化为HH:mm:ss
有没有一种很好的方法可以将持续时间格式化为hh:mm:ss,而不必处理时区 我试过这个:Dart 将持续时间格式化为HH:mm:ss,dart,flutter,Dart,Flutter,有没有一种很好的方法可以将持续时间格式化为hh:mm:ss,而不必处理时区 我试过这个: DateTime durationDate = DateTime.fromMillisecondsSinceEpoch(0); String duration = DateFormat('hh:mm:ss').format(durationDate); 但我总是有1小时的时间,在这种情况下,它会说01:00:00 当我这样做的时候: Duration(milliseconds: 0).toString()
DateTime durationDate = DateTime.fromMillisecondsSinceEpoch(0);
String duration = DateFormat('hh:mm:ss').format(durationDate);
但我总是有1小时的时间,在这种情况下,它会说01:00:00
当我这样做的时候:
Duration(milliseconds: 0).toString();
我得到这个:
0:00:00.000000
您可以使用Duration
并实现这个方法:
String _printDuration(Duration duration) {
String twoDigits(int n) => n.toString().padLeft(2, "0");
String twoDigitMinutes = twoDigits(duration.inMinutes.remainder(60));
String twoDigitSeconds = twoDigits(duration.inSeconds.remainder(60));
return "${twoDigits(duration.inHours)}:$twoDigitMinutes:$twoDigitSeconds";
}
用法:
final now = Duration(seconds: 30);
print("${_printDuration(now)}");
您可以自己开始创建格式,来吧:
String sDuration = "${duration.inHours}:${duration.inMinutes.remainder(60)}:${(duration.inSeconds.remainder(60))}";
您可以使用以下选项:
print('${duration.inHours.toString().padLeft(2,'0')):
${duration.inMinutes.rements(60).toString().padLeft(2,'0')}:
${duration.unseconds.rements(60.toString().padLeft(2,'0')}');
这是另一个版本。在这一点上,这都是首选,但我喜欢它是干的,不需要函数声明(包装函数显然是可选的),尽管它肯定有点函数链沉重
紧凑的
字符串格式时间(双倍时间){
持续时间=持续时间(毫秒:time.round());
返回[duration.inHours,duration.inMinutes,duration.inSeconds].map((seg)=>seg.rements(60).toString().padLeft(2,'0')).join(':');
}
格式化版本
String timeFormatter (double time) {
Duration duration = Duration(milliseconds: time.round());
return [duration.inHours, duration.inMinutes, duration.inSeconds]
.map((seg) => seg.remainder(60).toString().padLeft(2, '0'))
.join(':');
}
从
Duration
中获取HH:mm:ss
的最短、最优雅、最可靠的方法是:
format(Duration d)=>d.toString().split('.').first.padLeft(8,“0”);
用法示例:
main(){
最终d1=持续时间(小时:17,分钟:3);
最终d2=持续时间(小时:9,分钟:2,秒:26);
最终d3=持续时间(毫秒:0);
打印(格式(d1));//17:03:00
打印(格式(d2));//09:02:26
打印(格式(d3));//00:00:00
}
只是一个快速实现
这将以[DD]d:[HH]h:[mm]m:[ss]s格式显示持续时间,如果前导元素为0,则将忽略它。但是秒总是存在的
例如:
1d:2h:3m:4s
2h:3m:4s
3m:4s
4s
0
///返回给定持续时间[d]内的格式化字符串为DD:HH:mm:ss
///如果为0,则忽略。
静态字符串格式持续时间(持续时间d){
var秒=d.秒;
最后几天=秒~/Duration.secondsPerDay;
秒-=天*Duration.secondsPerDay;
最后的小时=秒~/持续时间。秒;
秒-=小时*Duration.secondsPerHour;
最后分钟=秒~/持续时间。秒分钟;
秒-=分钟*持续时间。秒分钟;
最终列表标记=[];
如果(天!=0){
添加('${days}d');
}
if(tokens.isNotEmpty | | hours!=0){
添加('${hours}h');
}
if(tokens.isNotEmpty | |分钟!=0){
add('${minutes}m');
}
add('${seconds}s');
返回令牌。join(“:”);
}
根据@diegoveloper的回答,我将其作为一个扩展,它也是可扩展的
extension DurationExtensions on Duration {
/// Converts the duration into a readable string
/// 05:15
String toHoursMinutes() {
String twoDigitMinutes = _toTwoDigits(this.inMinutes.remainder(60));
return "${_toTwoDigits(this.inHours)}:$twoDigitMinutes";
}
/// Converts the duration into a readable string
/// 05:15:35
String toHoursMinutesSeconds() {
String twoDigitMinutes = _toTwoDigits(this.inMinutes.remainder(60));
String twoDigitSeconds = _toTwoDigits(this.inSeconds.remainder(60));
return "${_toTwoDigits(this.inHours)}:$twoDigitMinutes:$twoDigitSeconds";
}
String _toTwoDigits(int n) {
if (n >= 10) return "$n";
return "0$n";
}
}
在详细阐述其他答案时,以下是一个实现,该实现还可以格式化日期:
extension DurationFormatter on Duration {
/// Returns a day, hour, minute, second string representation of this `Duration`.
///
///
/// Returns a string with days, hours, minutes, and seconds in the
/// following format: `dd:HH:MM:SS`. For example,
///
/// var d = new Duration(days:19, hours:22, minutes:33);
/// d.dayHourMinuteSecondFormatted(); // "19:22:33:00"
String dayHourMinuteSecondFormatted() {
this.toString();
return [
this.inDays,
this.inHours.remainder(24),
this.inMinutes.remainder(60),
this.inSeconds.remainder(60)
].map((seg) {
return seg.toString().padLeft(2, '0');
}).join(':');
}
}
不幸的是,intl
包DateFormat
类没有帮助:它将持续时间的格式标记为未实现:
formatDuration(DateTime reference) → String
NOT YET IMPLEMENTED. [...]
定义如下:
持续时间的延长{
字符串格式();
}
用法:
final now = Duration(seconds: 30);
print("${_printDuration(now)}");
String time=Duration(秒:3661).format();//01:01:01
我更喜欢把毫秒看作它自己的单位,而不是它的一个子单位
还有别的。从这个意义上讲,它的值为0-999,因此
我想填三个而不是两个,就像我在其他答案中看到的那样。这是
实施:
String format(Duration o) {
var mil_s = (o.inMilliseconds % 1000).toString().padLeft(3, '0');
var sec_s = (o.inSeconds % 60).toString().padLeft(2, '0');
return o.inMinutes.toString() + ' m ' + sec_s + ' s ' + mil_s + ' ms';
}
在我看来,最简单的方法
String get refactoredDuration{
return Duration(seconds: duration).toString().split('.')[0];
}
谢谢,这是干净的飞镖。你知道我第一种方法的问题是什么吗?只要检查一下文档,你就会明白:如果你想把它缩短:stringtwodights(intn)=>n>=10?“$n”:“0$n”
或justn.toString().padLeft(2,“0”)
getAudioDuration(Duration Duration){String twoDigits(int n)=>n.toString().padLeft(2,“0”);String twoDigitMinutes=twoDigits(Duration.inMinutes.余数(60));String twoDigitSeconds=twoDigits(Duration.inSeconds.余数(60));列表输出=[];如果(duration.inHours>0){output.add(两位数(duration.inHours.toString());}if(int.parse(twoDigitSeconds)>0){output.add(twoDigitMinutes);output.add(twoDigitSeconds);}返回output.join(':');}duration.inMinutes是总分钟数,例如,如果持续时间为2小时,分钟数将为120嘿,谢谢。我不知道它超过60分钟。我相信你的答案。为此,我在库中找到了余数方法,并引入了它,将其限制为60分钟。效果很好,但如果你想保留前导零,则会丢失前导零只需为每个相关数据(小时、分钟、秒)添加.toString().padLeft(2,'0')
,我非常喜欢这个实现。我稍微修改了一下,将[[DD:]hh:]mm]:ss
显示为零,例如通过替换add as标记来显示“1:02:03”。add(addZeroIfFirst(hours,tokens.isEmpty))
和字符串addZeroIfFirst(int值,bool isFirst){返回sprintf((isFirst)“%d”:“%02d”,[value]);}
。这使用了.FYI中的sprintf
,您的方法不起作用,因为尽管有变量名,但您没有计算持续时间。durationDate
是一个DateTime
;它是一个时间点,因此需要从UTC调整到您的本地时区。
String get refactoredDuration{
return Duration(seconds: duration).toString().split('.')[0];
}