austbot
8/3/2015 - 3:44 PM

Format Moment Duration easily

Format Moment Duration easily

function (duration, format) {
  //Setup time
  var hours = duration.hours(),
    minutes = duration.minutes(),
    seconds = duration.seconds();
  //Iterate the format
  formatStr = format;
  //Replace format keys
  formatStr = formatStr.replace(/hh:/, hours > 0 ? hours + ":" : '');
  formatStr = formatStr.replace(/mm:/, minutes > 0 ? minutes + ":" : '');
  formatStr = formatStr.replace(/ss/, seconds > 0 ? seconds : '0');
  return formatStr;
};