簡體   English   中英

Javascript 在 IOS 上返回 NAN NAN NAN

[英]Javascript Returning NAN NAN NAN on IOS

我正在使用此函數根據當前用戶時區將 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>

當我嘗試查看顯示 NAN NAN NAN NAN 的頁面時,該代碼在除 IOS 之外的所有平台上都能完美運行,問題是我無法在 ios safari 上調試代碼(我的 IPHONE 沒有 USB 電纜) . 有人可以幫我嗎? 謝謝

我強烈建議您使用moment JS使用format方法使此代碼可讀。

或者至少更像那樣寫

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)
      ;
};

所以你得到了NAN 我的猜測是time變量的問題。 它來自el.innerHTML ,一個用 parseInt 轉換的字符串。

您可以嘗試顯示它以驗證該值嗎? 就用這個小小的修改?

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

一切都是正確的,你應該考慮這樣一個事實,即這段代碼不能運行多次......或者它會因為parseInt('12/10/20 04:22)類的東西而失敗......所以也許添加一個'check no第二次運行'可能會有所幫助。

問題源於 iOS 試圖變得非常聰明——並認識到你的時間字符串可能是電話號碼。

長度與標准的美國電話號碼相匹配,因此 iOS 將它們包裝為鏈接,允許用戶單擊和呼叫。 1604263177變成<a href="tel:1604263177">1604263177</a>等等。 我懷疑 Android 和其他手機可能會顯示類似的行為。

最簡單的解決方案是從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

更新:

問題是 safari 自動將數字轉換為鏈接。

我通過使用解決了這個問題:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM