O&#39;jan G - 2 months ago 8
Javascript Question

Number of seconds in year changed

I have written this function to convert inputted seconds into an output for a user that is in Years, Days, Hours, Minutes, and Seconds. I am still working on the various outputs and I know their is a better way to syntax this; however, for now I am looking for a calculation error I get when conversions get into years. I am a few minutes off, can anybody spot this error? I appreciate it. Also if you have suggestions on my formatting, I would really appreciate it. I was considering a switch statement, but wasn't sure what to use as a input condition.

Please debug with the function input => formatDuration(69771311);

``````function formatDuration(n) {

var arr=[~~(n/31536000 ),~~(n%31536000 /86400)%365,~~(n%31536000%86400/3600)%24,~~(n%31536000 %86400%3600/60)%60,n%31536000 %86400%3600%60%60];
console.log(arr);

if(arr[4] <= 0 && arr[1] == 0 && arr[2] == 0 && arr[3] == 0)
return 'now';
//Single Second
else if( arr[4] == 1 && arr[1] == 0 && arr[2] == 0 && arr[3] == 0 && arr[0] == 0 )
return arr[4]+' second';
//Single minute, multiple seconds
else if( arr[4] > 1 && arr[3] == 1 && arr[2] == 0 && arr[1] == 0 )
return arr[3]+' minute and '+arr[4]+' seconds';
//Multiple Seconds
else if( arr[4] > 1 && arr[1] == 0 && arr[2] == 0 && arr[3] == 0 )
return arr[4]+' seconds';
//Single Minute
else if(arr[4] == 0 && arr[3] == 1 && arr[2] == 0 && arr[1] == 0 )
return arr[3]+' minute';
//Multiple Minutes
else if(arr[4] == 0 && arr[3] > 1 && arr[2] == 0 && arr[1] == 0 )
return arr[3]+' minutes';
//Single Second and Single Minute
else if(arr[4] == 1 && arr[3] == 1 && arr[2] == 0 && arr[1] == 0 )
return arr[3]+' minute and ' + arr[4] + ' second';
//Multiple minutes and multiple seconds
else if(arr[4] > 1 && arr[3] > 1 && arr[2] == 0 && arr[1] == 0 )
return arr[3]+' minutes and ' + arr[4] + ' seconds';
//Multiple minutes and single seconds
else if( arr[4] > 1 && arr[3] > 1 && arr[2] == 0 && arr[1] == 0 )
return arr[3]+' minutes and ' + arr[4] + ' seconds';
//Single hour, multiple minutes, and single second
else if( arr[4] > 1 && arr[3] > 1 && arr[2] == 1 && arr[1] == 0 )
return arr[2]+' hour, ' + arr[3] + ' minutes and '+arr[4]+' seconds';
//Single hour, single minute
else if( arr[4] == 0 && arr[3] == 1 && arr[2] == 1 && arr[1] == 0 )
return arr[2]+' hour and ' + arr[3] + ' minute';
//Single hour and single minute
else if( arr[4] == 1 && arr[3] == 1 && arr[2] == 0 && arr[1] == 0 )
return arr[2]+' hour and' + arr[3] + ' minute';
//Single hour, single minute and single second
else if( arr[4] == 1 && arr[3] == 1 && arr[2] == 1 && arr[1] == 0 )
return arr[2]+' hour,  ' + arr[3] + ' minute and ' +arr[4]+ ' second';
//Single hour and single second
else if( arr[4] == 1 && arr[3] == 0 && arr[2] == 1 && arr[1] == 0 )
return arr[2]+' hour and ' +arr[4]+ ' second ';
//single hour
else if( arr[4] == 0 && arr[3] == 0 && arr[2] == 1 && arr[1] == 0 )
return arr[2]+' hour';
//Single hour, single minute and multiple second
else if( arr[4] > 1 && arr[3] == 1 && arr[2] == 1 && arr[1] == 0 )
return arr[2]+' hour, ' + arr[3] + ' minute and ' +arr[4]+ ' seconds';
//Multiple days, single hour, multiple minutes and multiple seconds
else if( arr[4] > 1 && arr[3] > 1 && arr[2] == 1 && arr[1] > 1 && arr[0] == 0 )
return arr[1]+' days, '+arr[2]+' hour, ' + arr[3] + ' minutes and ' +arr[4]+ ' seconds';
//Single day, single hour, multiple minutes and multiple seconds
else if( arr[4] > 1 && arr[3] > 1 && arr[2] == 1 && arr[1] > 0 && arr[0] == 0 )
return arr[1]+' days, '+arr[2]+' hour, ' + arr[3] + ' minutes and ' +arr[4]+ ' seconds';
//multiple days, multiple hours, multiple minutes and multiple seconds
else if( arr[4] > 1 && arr[3] > 1 && arr[2] > 1 && arr[1] > 1 && arr[0] == 0 )
return arr[1]+' days, '+arr[2]+' hours, ' + arr[3] + ' minutes and ' +arr[4]+ ' seconds';
//Multiple years, multiple days, single hour, multiple minutes and multiple seconds
else if( arr[4] > 1 && arr[3] > 1 && arr[2] == 1 && arr[1] > 1 && arr[0] > 0 )
return arr[0]+' years, ' + arr[1]+' days, '+arr[2]+' hour, ' + arr[3] + ' minutes and ' +arr[4]+ ' seconds';
//single years, multiple days, single hour, multiple minutes and multiple seconds
else if( arr[4] > 1 && arr[3] > 1 && arr[2] == 1 && arr[1] > 1 && arr[0] == 1 )
return arr[0] + ' year, ' +arr[1]+' days, '+arr[2]+' hour, ' + arr[3] + ' minutes and ' +arr[4]+ ' seconds';
//single years, multiple days, multiple hour, multiple minutes and multiple seconds
else if( arr[4] > 1 && arr[3] > 1 && arr[2] > 1 && arr[1] > 1 && arr[0] == 1 )
return arr[0] + ' year, ' +arr[1]+' days, '+arr[2]+' hours, ' + arr[3] + ' minutes and ' +arr[4]+ ' seconds';
//multiple years, multiple days, multiple hours, multiple minutes 0 seconds
else if( arr[4] > 1 && arr[3] > 1 && arr[2] > 1 && arr[1] > 1 && arr[0] == 0 )
return arr[0] + ' years, ' +arr[1]+' days, '+arr[2]+' hours, and ' + arr[3] + ' minutes';
//multiple years, multiple days, multiple hours, multiple minutes nd single second
else if( arr[4] > 1 && arr[3] > 1 && arr[2] > 1 && arr[1] > 1 && arr[0] == 1 )
return arr[0] + ' years, ' +arr[1]+' days, '+arr[2]+' hours, ' + arr[3] + ' minutes and ' + arr[4]+' second';
//multiple years, multiple days, multiple hours, multiple minutes and multiple seconds
else if( arr[4] > 1 && arr[3] > 1 && arr[2] > 1 && arr[1] > 1 && arr[0] > 1 )
return arr[0] + ' years, ' +arr[1]+' days, '+arr[2]+' hours, ' + arr[3] + ' minutes and '+arr[4] + ' seconds';
//multiple years, multiple days, multiple hours, multiple minutes and multiple seconds
else if( arr[0] > 1 && arr[1] > 1 && arr[2] > 1 && arr[3] > 1 && arr[4] == 0 )
return arr[0] + ' years, ' +arr[1]+' days, '+arr[2]+' hours and ' + arr[3] + ' minutes';
//multiple years, multiple days, multiple hours, multiple minutes and multiple seconds
else if( arr[0] > 1 && arr[1] > 1 && arr[2] > 1 && arr[3] > 1 && arr[4] == 1 )
return arr[0] + ' years, ' +arr[1]+' days, '+arr[2]+' hours, ' + arr[3] + ' minutes and ' + arr[4] + ' second';
//multiple years, multiple days, multiple hours, multiple minutes and multiple seconds
else if( arr[0] > 1 && arr[1] > 1 && arr[2] > 1 && arr[3] > 1 && arr[4] == 1 )
return arr[0] + ' years, ' +arr[1]+' days, '+arr[2]+' hours, ' + arr[3] + ' minutes and ' + arr[4] + ' second';
}
``````

If i got it right you might do this job as follows;

``````var formatDuration = n => [~~(n/31556926),~~(n%31556926/86400)%365,~~(n%31556926%86400/3600)%24,~~(n%31556926%86400%3600/60)%60,n%31556926%86400%3600%60%60];
console.log(formatDuration(69771311));``````

Where returned array's

``````index 0: # of years,
index 1: # of days,
index 2: # of hours,
index 3: # of minutes,
index 4: # of seconds,
``````
Source (Stackoverflow)