Flutter / Dart - Convert seconds to Duration?

in #flutter2 years ago

Have you ever ran in to following problem?
Let's say you are given 2348 seconds, what's the actual length of content?

Run following code snippet & you'll get the answer.

import 'dart:developer';

class Utilities {

  static String formatTime(int seconds) {
    return '${(Duration(seconds: seconds))}'
        .split('.')[0]
        .padLeft(8, '0');
  }

}

log("For 2348 seconds - Duration is = ${Utilities.formatTime(2348)}");

// it would print as follows
// For 2348 seconds - Duration is = 00:39:08