現在のローカル時間とサーバ時間をjsで取得します.

3131 ワード

ローカル時間を取得:

// setInterval("labTime.innerText=new Date().toLocaleString()", 1000) // 1000 ( 1 )

サーバ時間の取得:
 var xmlHttp = false;
    //       
    try {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e2) {
            xmlHttp = false;
        }
    }


    if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
        xmlHttp = new XMLHttpRequest();
    }


    xmlHttp.open("GET", "null.txt", false);
    xmlHttp.setRequestHeader("Range", "bytes=-1");
    xmlHttp.send(null);


    severtime = new Date(xmlHttp.getResponseHeader("Date"));


    //       
    var year = severtime.getFullYear();
    var month = severtime.getMonth() + 1;
    var date = severtime.getDate();
    //       
    var hour = severtime.getHours();
    var minu = severtime.getMinutes();
    var seco = severtime.getSeconds();
    var week = '   ' + '       '.charAt(severtime.getDay());
	
    //          
    function getSeverTime() {
        seco++;
        if (seco == 60) {
            minu += 1;
            seco = 0;
        }
        if (minu == 60) {
            hour += 1;
            minu = 0;
        }
        if (hour == 24) {
            date += 1;
            hour = 0;
        }
        //    
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            if (date == 32) {
                date = 1;
                month += 1;
            }
        } else if (month == 4 || month == 6 || month == 9 || month == 11) {
            if (date == 31) {
                date = 1;
                month += 1;
            }
        } else if (month == 2) {
            if (year % 4 == 0 && year % 100 != 0) {//    
                if (date == 29) {
                    date = 1;
                    month += 1;
                }
            } else {
                if (date == 28) {
                    date = 1;
                    month += 1;
                }
            }
        }
        if (month == 13) {
            year += 1;
            month = 1;
        }
        sseco = addZero(seco);
        sminu = addZero(minu);
        shour = addZero(hour);
        sdate = addZero(date);
        smonth = addZero(month);
        syear = year;


        innerdata = "    :";
        document.getElementById("servertime").innerHTML = innerdata + syear + "-" + smonth + "-" + sdate + " " + shour + ":" + sminu + ":" + sseco + " " + week;
        setTimeout("getSeverTime()", 1000);
        setTimeout("getClientTime()", 100);
    }


    function addZero(num) {
        num = Math.floor(num);
        return ((num <= 9) ? ("0" + num) : num);
    }
// body //