0

In my database, I am storing dates as Unix time strings (seconds since the Unix epoc). I print these strings into a table, then use JavaScript's new Date() to automatically convert them into human-readable dates. The issue is, using the resulting Date object returns an "Invalid Date" message.

This is the script I use to convert Unix timestamps into dates:


    const timestamps = document.getElementsByClassName("utime"); // Elements with class name 'utime' are time elements which contain the timestamp as plain text

    for (let i = 0; i < timestamps.length; i++) {
        var timestamp = timestamps[i].innerText;
        console.log(timestamp);

        var formattedTime = new Date(timestamp);

        timestamps[i].innerText = formattedTime;
    }

When I do this, the elements display "Invalid Date" as their text.

AcinonX
  • 51
  • 4

0 Answers0