Skip to content

Instantly share code, notes, and snippets.

@Spittal
Created July 31, 2014 22:27
Show Gist options
  • Save Spittal/549fb0e3dbc06f6c0c74 to your computer and use it in GitHub Desktop.
Save Spittal/549fb0e3dbc06f6c0c74 to your computer and use it in GitHub Desktop.
Angular Time Filter that takes milliseconds and returns it as full minutes and seconds. For example: 60000 = 1:00, 6000000 = 100:00, 6000000 = 1:40:00 (withHour = true)
.filter('formatTime', function() {
return function(milliseconds,withHour) {
var seconds = parseInt((milliseconds/1000)%60);
var minutes = parseInt((milliseconds/(1000*60))%60);
var hours = parseInt((milliseconds/(1000*60*60))%24);
var out = "";
minutes = (parseInt(minutes) + (60 * parseInt(hours)));
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
out = minutes + ":" + seconds;
if(withHour) {
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
out = hours + ":" + minutes + ":" + seconds;
}
return out;
};
});
@jordaofranca
Copy link

Your code is adding an extra "0" to the time withHour, please move the first step to a else after the if

.filter('formatTime', function() {
return function(milliseconds,withHour) {
var seconds = parseInt((milliseconds/1000)%60);
var minutes = parseInt((milliseconds/(1000_60))%60);
var hours = parseInt((milliseconds/(1000_60*60))%24);
var out = "";

    if(withHour) {
        hours = (hours < 10) ? "0" + hours : hours;
        minutes = (minutes < 10) ? "0" + minutes : minutes;
        seconds = (seconds < 10) ? "0" + seconds : seconds;

        out = hours + ":" + minutes + ":" + seconds;
    }else {
        minutes = (parseInt(minutes) + (60 * parseInt(hours)));
        minutes = (minutes < 10) ? "0" + minutes : minutes;
        seconds = (seconds < 10) ? "0" + seconds : seconds;

        out = minutes + ":" + seconds;
    }

    return out;
};

});

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