Created
April 15, 2016 08:46
-
-
Save napoli1890/5f2d1383118049f5d385bb38b830168b to your computer and use it in GitHub Desktop.
AngularJS Filter that convert Seconds to Time String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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