Javascript Returning NAN NAN NAN on IOS

I strongly recommend you to use moment JS to make this code readable using the format method.

Or write it at least more like that

function formatTime(time) {
   let d = new Date(((time + timeConversion) * 1000));
   return
      ('0' + d.getUTCDate()).slice(-2)
      + '/' +
      ('0' + (d.getUTCMonth() + 1) ).slice(-2)
      + '/' +
      d.getUTCFullYear().toString().substr(2, 4)
      + ' ' + 
      ('0' + d.getUTCHours()).slice(-2)
      + ':' +
      ('0' + d.getUTCMinutes()).slice(-2)
      ;
};

So you got NAN. My guess is a problem with time variable. It comes from el.innerHTML, a string converted with parseInt.

Could you try displaying it to verify the value? Just with this small modification?

Array.prototype.forEach.call(allDates, function(el) {
   const before = el.innerHTML;
   const after = formatTime(parseInt(el.innerHTML));
   el.innerHTML = `${before} // ${after}`;
   el.className = "candidatedate";
});

It everything is correct, you should maybe consider the fact that this code cannot run more than once… or it will fail with something like parseInt('12/10/20 04:22).. So maybe adding a ‘check no second run’ could help.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top