日期框中的时区问题

时间:2014-07-13 20:33:42

标签: jquery-mobile timezone datebox

我对这段代码有一个奇怪的问题......它在我的电脑上工作正常,但在我的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>  

1 个答案:

答案 0 :(得分: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");