Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting a Duration like HH:mm:ss

Tags:

flutter

dart

Is there a good way to format a Duration in something like hh:mm:ss, without having to deal with time zones?

I tried this:

DateTime durationDate = DateTime.fromMillisecondsSinceEpoch(0);
String duration = DateFormat('hh:mm:ss').format(durationDate);

But I always get 1 hour to much, in this case it would say 01:00:00

And When I do this:

Duration(milliseconds: 0).toString();

I get this: 0:00:00.000000

like image 886
Jonas Avatar asked Feb 19 '19 21:02

Jonas


People also ask

How do you write time in HH MM format?

For example, the HHMM w.d. format writes 9:00 instead of 09:00. is an integer between 00 and 59 that represents minutes. SAS rounds hours and minutes that are based on the value of seconds in a SAS time value.

What does HH mm format mean?

HHMM means clock time in hour and minute format, with no separators.

What is HH MM 24 hour format?

A time of day is written in the 24-hour notation in the form hh:mm (for example 01:23) or hh:mm:ss (for example, 01:23:45), where hh (00 to 23) is the number of full hours that have passed since midnight, mm (00 to 59) is the number of full minutes that have passed since the last full hour, and ss (00 to 59) is the ...


2 Answers

You can use Duration and implement this method:

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

Usage:

final now = Duration(seconds: 30);
print("${_printDuration(now)}");
like image 164
diegoveloper Avatar answered Oct 19 '22 09:10

diegoveloper


You can start creating a format yourself, come on this one:

String sDuration = "${duration.inHours}:${duration.inMinutes.remainder(60)}:${(duration.inSeconds.remainder(60))}"; 
like image 41
AlexPad Avatar answered Oct 19 '22 11:10

AlexPad