dreami dreami - 4 months ago 11
Javascript Question

How can I interpret the time with less code?

Moved to stackexchange:
http://codereview.stackexchange.com/questions/135436/how-can-i-interpret-the-time-with-less-code

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.

if(json.Data.length>0){
for(var i=0;i<=json.Data.length-1;i++)
{
s += '<tr><td>' + data[i].data1+ '</td>';
s += '<td>'+ data[i].data2+ '</td>';
if(data[i].data3== null) {
s += '<td>N / A</td>' +
'<td>Has not logged in.</td>';
}
else {
s += '<td>' + moment(data[i].data3).format('DD MMM YYYY hh:mm A') + '</td>';

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;
}
}
}
}
$('#bodytable').empty().append(s);


What really matters here is from line

var time = data[i].data4;


and on.

For some context:
data[i].data4
is from a column that is an operation of

DATEDIFF(mi, Column3, 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'.

Answer

I'm not 100% certain about your JSON object, but you can use momentjs#fromNow to get the relative time string.

Which super simplifies the code.

if(json.Data.length>0){
    for(var i=0;i<=json.Data.length-1;i++) { 
        s += '<tr><td>' + data[i].data1+ '</td>';
        s += '<td>'+ data[i].data2+ '</td>';
        if(data[i].data3== null) {
            s += '<td>N / A</td>' +
                '<td>Has not logged in.</td>';
        }
        else {
            s += '<td>' + moment(data[i].data3).format('DD MMM YYYY hh:mm A') + '</td>';
            // not 100% sure what this value is.
            s += '<td>last login ' + moment(data[i].data4).fromNow() +'</td>'
            // will output:
            // last login 15 seconds ago
            // last login 4 years ago
            // last login 3 weeks ago
        }
    }
}
$('#bodytable').empty().append(s);

If you need the exact formatted strings, you can update them in momentjs.

moment.updateLocale('en', {
    relativeTime : {
        past:   "%s"
    }
});

// then it would look like:
s += '<td>' + moment(data[i].data4).fromNow() +' since last login</td>';
// 5 minutes since last login
// a hour since last login
// 187 years since last login
Comments