Ask coding questions

← Back to all posts
In my Chatroom, I receive a lot of "NaN's ago" as the time.
h
UnluckyFroggy (917)

In my Chatroom, I receive a lot of "NaN's ago" as the time. Normally it happens with the past messages from before a restart of the repl.

Here is a code snippet, tell me if something is wrong with this code.

    var timeDiff = time - d.time;
    var timeStr = "";
    var secDiff = Math.floor(timeDiff / 1000);
    var minDiff = Math.floor(timeDiff / (60 * 1000));
    var hourDiff = Math.floor(timeDiff / (60 * 60 * 1000));
    var dayDiff = Math.floor(timeDiff / (24 * 60 * 60 * 1000));
    if (minDiff >= 60) minDiff = minDiff % 60;
    if (hourDiff >= 24) hourDiff = hourDiff % 24;
    if (secDiff >= 60) secDiff = secDiff % 60;
    timeStr = (dayDiff ? dayDiff + "d " : "") + (hourDiff ? hourDiff + "h " : "") + (minDiff ? minDiff + "m " : "") + (!minDiff ? secDiff + "s " : "") + "ago";
    html += '<div class="message">' + d.message.trim() + ' <span class="time">' + timeStr + '</span>' + (d.id ? '<button class="delete" id="' + d.id + '">Delete</button>' : "") + '</div>';
Comments
hotnewtop
ch1ck3n (1622)

Youre probably not storing the times for the messages. if you're just hardcoding some HTML into a file it should be easy.

UnluckyFroggy (917)

@ch1ck3n chats.txt, you were legit a dev, you should know that.