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 if
s 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 = '<td>' + time + ' minutes since last login.</td></tr>';
if(time >= 60) {
var hour= tiempo / 60;
summary = '<td>' + Math.floor(hora) + ' hours since last login.</td></tr>';
if(hour>= 24) {
var day = hour/ 24;
summary= '<td>' + Math.floor(day) + ' days since last login.</td></tr>';
if(dia >= 7) {
var week= day / 7;
summary = '<td>' + Math.floor(week) + ' weeks since last login.</td></tr>';
if(week >= 4) {
var month = week / 4;
summary = '<td>' + Math.floor(mes) + ' months since last login.</td></tr>';
}
}
}
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'.