Skip to content

Instantly share code, notes, and snippets.

@napoli1890
Created April 15, 2016 08:46
Show Gist options
  • Save napoli1890/5f2d1383118049f5d385bb38b830168b to your computer and use it in GitHub Desktop.
Save napoli1890/5f2d1383118049f5d385bb38b830168b to your computer and use it in GitHub Desktop.
AngularJS Filter that convert Seconds to Time String
app.filter('secondsToDateTime', [function() {
/**
* This code returns a date string formatted manually.
* Code "new Date(1970, 0, 1).setSeconds(seconds)" returns malformed output on days.
* Eg. 4 days, magically becomes 5, 15 becomes 16 and so on...;
* */
return function(seconds) {
var days = Math.floor(seconds/86400);
var hours = Math.floor((seconds % 86400) / 3600);
var mins = Math.floor(((seconds % 86400) % 3600) / 60);
var secs = ((seconds % 86400) % 3600) % 60;
return (days > 0 ? days+'d ' : '') + ('00'+hours).slice(-2) +':' + ('00'+mins).slice(-2)+':' + ('00'+secs).slice(-2);
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment