Created
November 2, 2014 17:00
-
-
Save electblake/66b6f79efb1a4eed4c03 to your computer and use it in GitHub Desktop.
timeFormat filter to accept seconds and format 00:00:00
This file contains hidden or 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
| 'use strict'; | |
| /** | |
| * @ngdoc filter | |
| * @name musiLandingApp.filter:timeFormat | |
| * @function | |
| * @description | |
| * # timeFormat | |
| * Filter in the musiLandingApp. | |
| */ | |
| angular.module('myAppFilters') | |
| .filter('timeFormat', ['$log', function ($log) { | |
| // taken from https://gist.github.com/monkeycycle/1398263 | |
| return function (secs, format) { | |
| var chai = window.chai, | |
| expect = chai.expect; | |
| var magnitude = '+'; | |
| try { | |
| expect(secs, 'Seconds').to.not.be.undefined; | |
| expect(secs, 'Seconds').to.not.equal(0); | |
| secs = parseInt(secs); | |
| if (secs < 0) { | |
| magnitude = '-'; | |
| secs = secs * -1; | |
| } | |
| var hr = Math.floor(secs / 3600); | |
| var min = Math.floor((secs - (hr * 3600))/60); | |
| var sec = Math.floor(secs - (hr * 3600) - (min * 60)); | |
| if (hr < 10) { hr = "0" + hr; } | |
| if (min < 10) { min = "0" + min; } | |
| if (sec < 10) { sec = "0" + sec; } | |
| if (hr) { hr = "00"; } | |
| if (format != null) { | |
| var formatted_time = format.replace('hh', hr); | |
| formatted_time = formatted_time.replace('h', hr*1+""); // check for single hour formatting | |
| formatted_time = formatted_time.replace('mm', min); | |
| formatted_time = formatted_time.replace('m', min*1+""); // check for single minute formatting | |
| formatted_time = formatted_time.replace('ss', sec); | |
| formatted_time = formatted_time.replace('s', sec*1+""); // check for single second formatting | |
| return formatted_time; | |
| } else { | |
| return magnitude+hr + ':' + min + ':' + sec; | |
| } | |
| } catch (err) { | |
| // $log.debug('- time filter fallback', err); | |
| return magnitude+'00:00:00'; | |
| } | |
| } | |
| }]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment