程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> js-時間換算的問題!!!!!!!!!! !!!!

js-時間換算的問題!!!!!!!!!! !!!!

編輯:編程解疑
時間換算的問題!!!!!!!!!! !!!!

時間換算 一天24個小時 比如說2016-12-31 23:50:50 這個加上2小時要變分鐘 要變小時 變天 變月份 變年 還有就是減的時候 2016-1-1 0:00 減去2小時 也要都變 我想問的是 我下面中的代碼邏輯 是不是寫錯了 錯在那裡 求指導
對了 北京時間是utc +8 那 utc+3.5 是小時+3 分鐘 +5 還是+30 還是什麼

在線急等 謝謝 之前也問過 有哥 給我寫了另一個簡單方法 沒看懂 沒弄明白 所在又問了一遍

最佳回答:


看我原來的那個帖子,獲取UTC時間字符串,每次計算都要 new 一個新的UTC事件,通過api更新時間對象後在調用方法設置時間,而不是直接操作
d.getUTCHours() + 3
這樣來操作,所以會顯示>24或者<0的情況出現

下面只改了部分,其他沒改的按照寫的示例修改

    function updateDate() {
        var d = new Date();
        var a = d.getUTCHours();
        //UTC時間字符串
        var sUTC = d.getUTCFullYear() + '/' + (d.getUTCMonth() + 1) + '/' + d.getUTCDate() + ' ' + d.getUTCHours() + ':' + d.getUTCMinutes() + ':' + d.getUTCSeconds();
        //世界標准時間
        $("#time").val(d.getUTCFullYear() + "年" + (d.getUTCMonth() + 1) + "月" + d.getUTCDate() + "日 " + d.getUTCHours() + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds());
        //當地時間
        $("#time1").val(d.getFullYear() + "年" + (d.getMonth() + 1) + "月" + d.getDate() + "日 " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds());
        //惠靈頓時間
        d = new Date(sUTC)//新UTC時間對象
        d.setHours(d.getHours() + 12);
        //因為是經過UTC計算得到的事件,就不需要在調用getUTCXXXXX這種方法了,直接調用getXXXX
        $("#time2").val(d.getFullYear() + "年" + (d.getMonth() + 1) + "月" + d.getUTCDate() + "日 " + d.getHours() + ":" + d.getMinutes() + ":" +d.getSeconds());
        //悉尼時間
        d = new Date(sUTC)//新UTC時間對象
        d.setHours(d.getHours() + 10);
        $("#time3").val(d.getFullYear() + "年" + (d.getMonth() + 1) + "月" + d.getUTCDate() + "日 " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds());


        ///////////////////////////下面的按照上面的改,分鐘的話用  d.setMinutes(d.getMinutes()+要增加的分鐘數),看新德裡的那個句
        //東京時間
        $("#time4").val(d.getUTCFullYear() + "年" + (d.getUTCMonth() + 1) + "月" + d.getUTCDate() + "日 " + (d.getUTCHours() + 9) + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds());
        //北京時間
        $("#time5").val(d.getUTCFullYear() + "年" + (d.getUTCMonth() + 1) + "月" + d.getUTCDate() + "日 " + (d.getUTCHours() + 8) + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds());
        //曼谷時間
        $("#time6").val(d.getUTCFullYear() + "年" + (d.getUTCMonth() + 1) + "月" + d.getUTCDate() + "日 " + (d.getUTCHours() + 7) + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds());


        //新德裡時間
        d = new Date(sUTC)//新UTC時間對象
        d.setHours(d.getHours() + 5);
        d.setMinutes(d.getMinutes() + 30);
        $("#time7").val(d.getFullYear() + "年" + (d.getMonth() + 1) + "月" + d.getUTCDate() + "日 " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds());



        //伊斯蘭堡時間
        $("#time8").val(d.getUTCFullYear() + "年" + (d.getUTCMonth() + 1) + "月" + d.getUTCDate() + "日 " + (d.getUTCHours() + 5) + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds());
        //德黑蘭時間
        $("#time9").val(d.getUTCFullYear() + "年" + (d.getUTCMonth() + 1) + "月" + d.getUTCDate() + "日 " + (d.getUTCHours() + 3) + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds());
        //巴格達時間
        $("#time10").val(d.getUTCFullYear() + "年" + (d.getUTCMonth() + 1) + "月" + d.getUTCDate() + "日 " + (d.getUTCHours() + 3) + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds());
        //耶路撒冷時間
        $("#time11").val(d.getUTCFullYear() + "年" + (d.getUTCMonth() + 1) + "月" + d.getUTCDate() + "日 " + (d.getUTCHours() + 2) + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds());
        //開普敦時間
        $("#time12").val(d.getUTCFullYear() + "年" + (d.getUTCMonth() + 1) + "月" + d.getUTCDate() + "日 " + (d.getUTCHours() + 2) + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds());
        //開羅時間
        $("#time13").val(d.getUTCFullYear() + "年" + (d.getUTCMonth() + 1) + "月" + d.getUTCDate() + "日 " + (d.getUTCHours() + 2) + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds());
        //卡薩布蘭卡時間
        $("#time14").val(d.getUTCFullYear() + "年" + (d.getUTCMonth() + 1) + "月" + d.getUTCDate() + "日 " + d.getUTCHours() + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds());
        //莫斯科時間
        $("#time15").val(d.getUTCFullYear() + "年" + (d.getUTCMonth() + 1) + "月" + d.getUTCDate() + "日 " + (d.getUTCHours() + 3) + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds());
        //雅典時間
        $("#time16").val(d.getUTCFullYear() + "年" + (d.getUTCMonth() + 1) + "月" + d.getUTCDate() + "日 " + (d.getUTCHours() + 2) + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds());
        //羅馬時間
        $("#time17").val(d.getUTCFullYear() + "年" + (d.getUTCMonth() + 1) + "月" + d.getUTCDate() + "日 " + (d.getUTCHours() + 1) + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds());
        //柏林時間
        $("#time18").val(d.getUTCFullYear() + "年" + (d.getUTCMonth() + 1) + "月" + d.getUTCDate() + "日 " + (d.getUTCHours() + 1) + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds());
        //巴黎時間
        $("#time19").val(d.getUTCFullYear() + "年" + (d.getUTCMonth() + 1) + "月" + d.getUTCDate() + "日 " + (d.getUTCHours() + 1) + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds());
        //倫敦時間
        $("#time20").val(d.getUTCFullYear() + "年" + (d.getUTCMonth() + 1) + "月" + d.getUTCDate() + "日 " + d.getUTCHours() + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds());


        //渥太華時間
        d = new Date(sUTC);
        d.setHours(d.getHours() - 5);
        $("#time21").val(d.getFullYear() + "年" + (d.getMonth() + 1) + "月" + d.getUTCDate() + "日 " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds());




        //墨西哥城時間
        $("#time22").val(d.getUTCFullYear() + "年" + (d.getUTCMonth() + 1) + "月" + d.getUTCDate() + "日 " + (d.getUTCHours() - 6) + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds());
        //紐約時間
        $("#time23").val(d.getUTCFullYear() + "年" + (d.getUTCMonth() + 1) + "月" + d.getUTCDate() + "日 " + (d.getUTCHours() - 5) + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds());
        //芝加哥時間
        $("#time24").val(d.getUTCFullYear() + "年" + (d.getUTCMonth() + 1) + "月" + d.getUTCDate() + "日 " + (d.getUTCHours() - 6) + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds());
        //洛杉矶時間
        $("#time25").val(d.getUTCFullYear() + "年" + (d.getUTCMonth() + 1) + "月" + d.getUTCDate() + "日 " + (d.getUTCHours() - 8) + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds());



    }
    window.onload = function () {

        setInterval(updateDate, 1000);
    }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved