(seconds)
| 1 | var secondsToStr = function(seconds) { |
| 2 | function numberEnding (number) { |
| 3 | return (number > 1) ? 's' : ''; |
| 4 | } |
| 5 | |
| 6 | var temp = seconds; |
| 7 | var res = ""; |
| 8 | var years = Math.floor(temp / 31536000); |
| 9 | if (years) { |
| 10 | res += years + ' year' + numberEnding(years) + ' '; |
| 11 | } |
| 12 | //TODO: Months! Maybe weeks? |
| 13 | var days = Math.floor((temp %= 31536000) / 86400); |
| 14 | if (days) { |
| 15 | res += days + ' day' + numberEnding(days) + ' '; |
| 16 | } |
| 17 | var hours = Math.floor((temp %= 86400) / 3600); |
| 18 | if (hours) { |
| 19 | res += hours + ' hour' + numberEnding(hours); |
| 20 | if (years) return res; |
| 21 | res += ' '; |
| 22 | } |
| 23 | var minutes = Math.floor((temp %= 3600) / 60); |
| 24 | if (minutes) { |
| 25 | res += minutes + ' minute' + numberEnding(minutes) + ' '; |
| 26 | if (days) return res; |
| 27 | res += ' '; |
| 28 | } |
| 29 | var seconds = Math.floor(temp % 60); |
| 30 | if (seconds) { |
| 31 | return res + seconds + ' second' + numberEnding(seconds); |
| 32 | } |
| 33 | if (res.length > 0) return res; |
| 34 | return 'less than a second'; //'just now' //or other string you like; |
| 35 | }; |
no test coverage detected