2014-07-13 78 views
0

我有一個奇怪的問題,這個代碼...它在我的電腦上工作正常,但在我的iPhone上的時間是+2小時...有人請看看我的代碼比如這裏...datebox中的時區問題

我一直試圖小時得到它的工作,但一切我嘗試似乎以同樣的方式結束了......

<html lang="en"> 
<head> 
    <title>Value is right on my PC, but not on my iPhone...?</title> 
     <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.3/jquery.mobile-1.4.3.min.css" /> 
     <link rel="stylesheet" href="http://dev.jtsage.com/cdn/datebox/latest/jqm-datebox.min.css" /> 

     <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script> 
     <script type="text/javascript" src="http://code.jquery.com/mobile/1.4.3/jquery.mobile-1.4.3.min.js"></script> 
     <script type="text/javascript" src="http://dev.jtsage.com/cdn/datebox/latest/jqm-datebox.core.min.js"></script> 
     <script type="text/javascript" src="http://dev.jtsage.com/cdn/datebox/latest/jqm-datebox.mode.datebox.min.js"></script> 

     <script type="text/javascript"> 
     window.onload = function() { date() }, setInterval(function() { date() }, 1000); 
     window.onload = function() { calculateTimlon() }, setInterval(function() { calculateTimlon() }, 1000); 

     var secondsDifference; 
     var minutesDifference; 
     var hoursDifference; 
     var totalTimeDifference; 
     var startTime; 
     var timeRightNow; 


     function date() { 
      timeRightNow = new Date(); 

      $('#MyDateInput').datebox({ 'overrideDateFormat': '%A %d-%B-%Y' }); 
      $('#MyTimeInput').datebox({ 'overrideDateFormat': '%H:%M:%S' }); 

      startTime = new Date($('#MyDateInput').datebox('callFormat', '%Y-%m-%d', $('#MyDateInput').datebox('getTheDate')) + "T" + $('#MyTimeInput').datebox('callFormat', '%H:%M:%S', $('#MyTimeInput').datebox('getTheDate'))); 
      totalTimeDifference = Math.abs(new Date(timeRightNow) - new Date(startTime)); 
      secondsDifference = Math.floor(totalTimeDifference/1000); 
      minutesDifference = Math.floor(secondsDifference/60); 
      secondsDifference = secondsDifference % 60; 
      hoursDifference = Math.floor(minutesDifference/60); 
      minutesDifference = minutesDifference % 60; 

      $("#hoursValue").html(hoursDifference); 
      $("#minutesValue").html(minutesDifference); 
      $("#secondsValue").html(secondsDifference); 

      $("#MyTimeValue").html(startTime); 
      $("#timeRightNowValue").html(timeRightNow); 
     } 

     function calculateTimlon() { 
      var timlon = 100; 
      var skatt = 0; 
      var semersat = 0; 

      var lonForeSkatt = ((semersat/100) + 1) * timlon; 
      var timskatt = (lonForeSkatt * (skatt/100)); 
      var lonEfterSkatt = lonForeSkatt - timskatt; 

      var minutlonEfterSkatt = lonEfterSkatt/60; 
      var sekundlonEfterSkatt = minutlonEfterSkatt/60; 

      var minutlonForeSkatt = lonForeSkatt/60; 
      var sekundlonForeSkatt = minutlonForeSkatt/60; 

      var sekundCounterEfterSkatt = (sekundlonEfterSkatt * totalTimeDifference)/1000; 
      var krEarnedFromStartEfterSkatt = sekundCounterEfterSkatt.toFixed(2); 

      var sekundCounterForeSkatt = (sekundlonForeSkatt * totalTimeDifference)/1000; 
      var krEarnedFromStartForeSkatt = sekundCounterForeSkatt.toFixed(2); 


      if (krEarnedFromStartEfterSkatt >= 0) { 
       $("#earnedFromStartValue").html(krEarnedFromStartEfterSkatt + "kr");     
      } 
     } 
    </script> 
</head> 
<body>  
    <div data-role="page" id="MyPage"> 

     <div data-role="content" id="MyPageContent" name="MyPageContent" data-position="fixed"> 
      <div class="ui-field-contain"> 
       <div id="MyStartTime"> 
        <input name="MyDateInput" id="MyDateInput" type="date" data-role="datebox" data-options='{"mode": "datebox", "overrideHeaderFormat":"%A %d-%B-%Y","useNewStyle":true, "useFocus":true,"overrideTimeFormat": 24, "closeCallback": "$(\"#MyTimeInput\").datebox(\"open\");"}' /> 
        <input name="MyTimeInput" id="MyTimeInput" type="time" data-role="datebox" data-options='{"mode": "timebox", "useNewStyle":true, "useFocus":true,"overrideTimeFormat": 24}' /> 
       </div> 
      </div> 

      <div id="timeRightNowText">My choosen startTime: <div id="MyTimeValue"></div></div> 

       <div id="hoursText">Hours: <div id="hoursValue"></div></div> 
       <div id="minutesText">Minutes: <div id="minutesValue"></div></div> 
       <div id="secondsText">Seconds: <div id="secondsValue"></div></div> 
       <div id="timeRightNowText">Time right now: <div id="timeRightNowValue"></div></div> 

       <div class="ui-block-b" id="valueFromStartTime">This value should start ticking from 0 if the time right now is the same as the time i choose above..<div id="earnedFromStartValue"></div></div> 
      </div> 
    </div> 
</body> 
</html> 
+0

可能重複的[HTML輸入類型日期時間本地設定了錯誤的時區(http://stackoverflow.com/questions/24703698/html-input-type-datetime-local-setting-the-錯誤時區) –

+0

請注意,這個問題和我作爲重複鏈接的唯一區別在於,您從datebox控件而不是datetime-local構建字符串。問題的其餘部分是相同的,相同的答案適用。 –

+0

你有工作嗎?我應該如何做的一個例子會很棒。 – user3771408

回答

0

我沒有改變我的timeRightNow喜歡的例子下面,現在它正在工作!看起來iPhone需要startTime和timeRightNow兩種看起來完全相同的方式來進行正確的計算。我在startTime結尾添加了「+02:00」,以便進行正確的時區計算。

var datetimeRightNow = new Date(); 

timeRightNow = new Date(datetimeRightNow.getFullYear() + '-' + (('' + (datetimeRightNow.getMonth() + 1)).length < 2 ? '0' : '') + (datetimeRightNow.getMonth() + 1) + '-' + (('' + datetimeRightNow.getDate()).length < 2 ? '0' : '') + datetimeRightNow.getDate() + "T" + (('' + datetimeRightNow.getHours()).length < 2 ? '0' : '') + datetimeRightNow.getHours() + ":" + (('' + datetimeRightNow.getMinutes()).length < 2 ? '0' : '') + datetimeRightNow.getMinutes() + ":" + (('' + datetimeRightNow.getSeconds()).length < 2 ? '0' : '') + datetimeRightNow.getSeconds() + "+02:00");