Interpreting the time EDIT: Alternate answer on overflow: https://stackoverflow.com/questions/38487692/how-can-i-interpret-the-time-with-less-code/38492144#38492144

But I really want to keep Jonah's answer here too. Just let me know if I really have to close either this one or stackoverflow's question.



I really don't like sticking many ifs together. I think it looks like a child stacking. But if anyone knows how to do the same thing I'm doing with this code but with less lines, it would be great.

var time = data[i].data4;
                    var summary = '&lt;td&gt;' + time + ' minutes since last login.&lt;/td&gt;&lt;/tr&gt;';
                    if(time &gt;= 60) {
                        var hour= tiempo / 60;
                        summary = '&lt;td&gt;' + Math.floor(hora) + ' hours since last login.&lt;/td&gt;&lt;/tr&gt;';
                        if(hour&gt;= 24) {
                            var day = hour/ 24;
                            summary= '&lt;td&gt;' + Math.floor(day) + ' days since last login.&lt;/td&gt;&lt;/tr&gt;';
                            if(dia &gt;= 7) {
                                var week= day / 7;
                                summary = '&lt;td&gt;' + Math.floor(week) + ' weeks since last login.&lt;/td&gt;&lt;/tr&gt;';
                                if(week &gt;= 4) {
                                    var month = week / 4;
                                    summary = '&lt;td&gt;' + Math.floor(mes) + ' months since last login.&lt;/td&gt;&lt;/tr&gt;';
                                }
                            }
                        }
                        s += summary;
                    } else {
                        s += summary;
                    }


For some context: data[i].data4 is from a column that is an operation of DATEDIFF(mi, someDate, getdate()).

As you can see, the returned results would be in minutes and from then on I start checking how many hours, days, and so on from that and I would even love to add between each line an (if it's greater than 1) so I can change the wording to singular instead of plural but I think that's too many lines just for a decision of whether to simply write 'minute' instead of 'minutes'.
Here's a rewrite without any ifs:

function humanTime(minutes) {
  var units          = ['months' , 'weeks' , 'days' , 'hours'];
  var minutesPerUnit = [40320    , 10080   , 1440   , 60];
  var timeInUnits    = minutesPerUnit.map(x =&gt; Math.floor(minutes / x));
  var time           = timeInUnits.find(x =&gt; x &gt; 0);
  var unit           = units[timeInUnits.indexOf(time)];
  return time + ' ' + unit;
}


Note I removed the &lt;td&gt;s from this code as it's mainly a view (formatting) concern.  This allows this function to remain more focused: it simply takes a number of minutes and converts it to hours/days/weeks/months as appropriate.

You might also consider returning an object of the form: {time: 6, unit: 'days'} instead of a string as I have, to make the separation of data and view even stronger.