Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shahr's avatar
Level 10

Why JavaScript see NaN?

blade

let sec_num = parseInt(this, 10);
let hours = Math.floor(sec_num / 3600);
let minutes = Math.floor((sec_num - (hours * 3600)) / 60);
let seconds = sec_num - (hours * 3600) - (minutes * 60);
if (hours < 10) {
    hours = "0" + hours;
}
if (minutes < 10) {
    minutes = "0" + minutes;
}
if (seconds < 10) {
    seconds = "0" + seconds;
}
console.log(hours + " : " + minutes + " : " + seconds);

I see this in my console

NaN : NaN : NaN

0 likes
22 replies
piljac1's avatar

We don't have the full code to help you, but this is probably an object in your case. Hence why you get NaN (not a number).

shahr's avatar
Level 10

@piljac1

blade

@foreach($products as $product)
	<p class="timestamp">{{ $product->timestamp }}</p>
@foreach

js

<script src="{{ asset('themes/js/jquery-3.6.1.min.js') }}"></script>
<script>
    String.prototype.toHHMMSS = function() {
        let sec_num = parseInt(this, 10);
        let hours = Math.floor(sec_num / 3600);
        let minutes = Math.floor((sec_num - (hours * 3600)) / 60);
        let seconds = sec_num - (hours * 3600) - (minutes * 60);
        if (hours < 10) {
            hours = "0" + hours;
        }
        if (minutes < 10) {
            minutes = "0" + minutes;
        }
        if (seconds < 10) {
            seconds = "0" + seconds;
        }
        console.log(hours + " : " + minutes + " : " + seconds)
        return hours + " : " + minutes + " : " + seconds;
    }
        
    let timestamps = document.getElementsByClassName("timestamp");
    Array.from(timestamps).forEach(function(timestamp, index) {
        setInterval(() => {
            let curTime = (new Date()).getTime() / 1000;
            document.getElementsByClassName('timestamp')[index].innerText = (`${timestamp-curTime}`).toHHMMSS();
        }, 1000)
    });
</script>
piljac1's avatar

@webaxin I'm assuming you want to display the elapsed time between the timestamp and now?

If so, there are multiple things wrong with your code, but I'm on my cellphone in bed right now. So I'll answer on my laptop during my lunch break if nobody did by then.

1 like
Sinnbeck's avatar

@webaxin Where is the link to your jsfiddle?

Copy the data into a jsfiddle so we can actually see the problem.

shahr's avatar
Level 10

@Sinnbeck https://jsfiddle.net/irankhosravi/qm0d6sb9/

In admin, I saved one to 2 and one to 1. I saved it with the timestamp method. It means one hour in the future and two hours in the future. So it should be shown like this. If I hit 1, it should show 00:59:59. And when I hit 2, it should show 01:59:59.

Tray2's avatar

@webaxin So it's something like this you need?

I suggest using Carbon and do it with php instead of with javaScript {{ $comment->created_at->diffForHumans() }}

function elapsed(time) {
        let sec_num = parseInt(time / 1000 );
        let hours = Math.floor(sec_num / 3600);
        let minutes = Math.floor((sec_num - (hours * 3600)) / 60);
        let seconds = sec_num - (hours * 3600) - (minutes * 60);
        if (hours < 10) {
            hours = "0" + hours;
        }
        if (minutes < 10) {
            minutes = "0" + minutes;
        }
        if (seconds < 10) {
            seconds = "0" + seconds;
        }
        return hours + " : " + minutes + " : " + seconds;
    }
        
    let timestamps = document.getElementsByClassName("timestamp");
    
    for (let i = 0; i < timestamps.length; i++) {
	  let curTime = Date.now() / 1000;
      timestamps.item(i).textContent = elapsed(curTime - parseInt(timestamps.item(i).textContent));
    }
    ```
shahr's avatar
Level 10

@Tray2

Your code is not working. I want seconds to change every second and minutes to change every minute and hours to change every hour. But the code you wrote is fixed.

piljac1's avatar
piljac1
Best Answer
Level 28

@webaxin If I properly understood what you want, this should do the trick.

@foreach($products as $product)
	<p data-timestamp="{{ $product->timestamp }}"></p>
@foreach
String.prototype.toHHMMSS = function() {
  const sec_num = parseInt(this, 10);
  let hours = Math.floor(sec_num / 3600);
  let minutes = Math.floor((sec_num - (hours * 3600)) / 60);
  let seconds = sec_num - (hours * 3600) - (minutes * 60);

  if (hours < 10) {
    hours = "0" + hours;
  }

  if (minutes < 10) {
    minutes = "0" + minutes;
  }

  if (seconds < 10) {
    seconds = "0" + seconds;
  }

  return hours + " : " + minutes + " : " + seconds;
}

const timestamps = document.querySelectorAll('[data-timestamp]');

timestamps.forEach(function(timestamp) {
  setInterval(() => {
    const curTime = (new Date()).getTime() / 1000;

    timestamp.innerText = (`${curTime - timestamp.dataset.timestamp}`).toHHMMSS();
  }, 1000);
});

To see the code in action, check out this CodePen

Changes made / explanation

  1. Changed unmodified variables from let to const.
  2. Swaped the timestamp class for a data-timestamp attribute, because it gives you the possibility to alter the paragraph's text content without losing the original timestamp.
  3. Swaped getElementsByClassName for querySelectorAll to be able to target the data attribute.
  4. Removed unnecessary Array.from and index.
  5. Removed unnecessary DOM elements refetching.
  6. Swapped the timestamp value with the curTime to get the appropriate time ellapsed since the timestamp.

P.S. If your JS code won't be compiled, make sure to change let and const with var and avoid arrow functions (and possibly use a for instead of forEach if you want to support below IE 11).

1 like
Tray2's avatar

@webaxin The code is working fine. The reason I removed the interval is that it would only work the first time, when the data is in timestamp format, on the second iteration it would be in 01:05:05 format and not 1758665.

Tray2's avatar

You are still trying to convert something that can't be converted to a number, so you need to make sure that you don't pass anything other than just numbers to your function.

Example:

function toInt(value) {
	return parseInt(value);
}

// Call the toInt function with a number and it will give you 12
toInt(12); 

// Call the toInt function with a numeric string and it will give you 12
toInt('12');

// Call the toInt function with a non numeric string and it will give NaN (Not a Number)
toInt('B12');

So make sure that the value is a number before you pass it.

Please or to participate in this conversation.