Skip to content

Instantly share code, notes, and snippets.

@austbot
Created August 3, 2015 15:44
Show Gist options
  • Save austbot/0c49a2809ec0cc694d08 to your computer and use it in GitHub Desktop.
Save austbot/0c49a2809ec0cc694d08 to your computer and use it in GitHub Desktop.
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;
};
@sahanDissanayake
Copy link

Can u give a quick example on how to use this please ? the format is confusing..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment