Formatting a Duration like HH:mm:ss

后端 未结 11 958
孤城傲影
孤城傲影 2020-12-05 22:53

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 = Da         


        
11条回答
  •  感情败类
    2020-12-05 23:47

    The shortest, most elegant and reliable way to get HH:mm:ss from a Duration is doing:

    format(Duration d) => d.toString().split('.').first.padLeft(8, "0");
    

    Example usage:

    main() {
      final d1 = Duration(hours: 17, minutes: 3);
      final d2 = Duration(hours: 9, minutes: 2, seconds: 26);
      final d3 = Duration(milliseconds: 0);
      print(format(d1)); // 17:03:00
      print(format(d2)); // 09:02:26
      print(format(d3)); // 00:00:00
    }
    

提交回复
热议问题