简体   繁体   English

Javascript 在 IOS 上返回 NAN NAN NAN

[英]Javascript Returning NAN NAN NAN on IOS

I am using this function to convert UTC timestamp to Date according to the current user timezone我正在使用此函数根据当前用户时区将 UTC 时间戳转换为日期

 let timeConversion = (new Date().getTimezoneOffset() * -1 / 60) * 60 * 60; function formatTime(time) { let d = new Date(((time + timeConversion) * 1000)); return (d.getUTCDate() < 10 ? '0' + d.getUTCDate() : d.getUTCDate()) + '/' + (d.getUTCMonth() + 1 < 10 ? '0' + (d.getUTCMonth() + 1) : (d.getUTCMonth() + 1)) + '/' + d.getUTCFullYear().toString().substr(2, 4) + ' ' + (d.getUTCHours() < 10 ? '0' + d.getUTCHours() : d.getUTCHours()) + ':' + (d.getUTCMinutes() < 10 ? '0' + d.getUTCMinutes() : d.getUTCMinutes()); } let allDates = document.getElementsByClassName("candidatedate"); Array.prototype.forEach.call(allDates, function(el) { el.innerHTML = formatTime(parseInt(el.innerHTML)); el.className = "candidatedate"; });
 <span class="candidatedate">1604263179</span><span class="candidatedate">1604263177</span><span class="candidatedate">1604263176</span>

The code works perfectly on every platform except for IOS when I try to see the page it shows NAN NAN NAN NAN and the issue is that i can't debug the code on ios safari(I don't have usb cable for my IPHONE).当我尝试查看显示 NAN NAN NAN NAN 的页面时,该代码在除 IOS 之外的所有平台上都能完美运行,问题是我无法在 ios safari 上调试代码(我的 IPHONE 没有 USB 电缆) . Can someone help me with that ?有人可以帮我吗? Thanks谢谢

I strongly recommend you to use moment JS to make this code readable using the format method.我强烈建议您使用moment JS使用format方法使此代码可读。

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 .所以你得到了NAN My guess is a problem with time variable.我的猜测是time变量的问题。 It comes from el.innerHTML , a string converted with parseInt.它来自el.innerHTML ,一个用 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.一切都是正确的,你应该考虑这样一个事实,即这段代码不能运行多次......或者它会因为parseInt('12/10/20 04:22)类的东西而失败......所以也许添加一个'check no第二次运行'可能会有所帮助。

The problem stems from iOS trying to be very clever — and recognizing that your time strings could possibly be phone numbers.问题源于 iOS 试图变得非常聪明——并认识到你的时间字符串可能是电话号码。

The length matches the standard US phone number so iOS wraps them as links, allowing the user to click and call.长度与标准的美国电话号码相匹配,因此 iOS 将它们包装为链接,允许用户单击和呼叫。 1604263177 becomes <a href="tel:1604263177">1604263177</a> and so forth. 1604263177变成<a href="tel:1604263177">1604263177</a>等等。 I suspect Android and other phones might display similar behavior.我怀疑 Android 和其他手机可能会显示类似的行为。

The easiest solution is to switch from innerHTML over to innexText .最简单的解决方案是从innerHTML切换到innexText

Array.prototype.forEach.call(allDates, function(el) {
  const input = parseInt(el.innerText, 10);
  el.innerHTML = input ? formatTime(input) : "";
  el.className = "candidatedate";
});

 function formatTime(time){return(new Date(time- (new Date().getTimezoneOffset()*60000) ))} //now basically 1 hr is 60000 units of the unit 'time' so that's my logic //also new Date(randomTimeStamp) produces a new date off of that recorded timestamp let d=formatTime(new Date().getTime()) console.log(d.toDateString()+" "+d.toLocaleTimeString()) //im not gonna copy a number when i can make one on the dot for illustration ;] //also, d is what the actual value u want, i just logged something that seems like it

UPDATE:更新:

The issue was that safari auto converted number to links.问题是 safari 自动将数字转换为链接。

I solved this by using :我通过使用解决了这个问题:

<meta name="format-detection" content="telephone=no">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM