MCPcopy Create free account
hub / github.com/cafebazaar/blacksmith / secondsToStr

Function secondsToStr

web/static/js/utils.js:1–35  ·  view source on GitHub ↗
(seconds)

Source from the content-addressed store, hash-verified

1var 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};

Callers 1

controllers.jsFile · 0.85

Calls 1

numberEndingFunction · 0.85

Tested by

no test coverage detected