我有一个奇怪的问题与这段代码…它在我的PC上运行得很好,但在我的iPhone上,时间是+2小时…有人可以请看看我的代码示例在这里…
我已经试了几个小时让它工作,但我所尝试的一切似乎都以同样的方式结束…
<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>
我确实像下面的例子一样改变了我的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");