상세 컨텐츠

본문 제목

javascript 서버시간 가져오기.

Programming/JavaScript

by 노란날. 2013. 4. 30. 17:45

본문

반응형

var xmlHttp;
function srvTime(){
    if (window.XMLHttpRequest) {//분기하지 않으면 IE에서만 작동된다.
        xmlHttp = new XMLHttpRequest(); // IE 7.0 이상, 크롬, 파이어폭스 등
        xmlHttp.open('HEAD',window.location.href.toString(),false);
        xmlHttp.setRequestHeader("Content-Type", "text/html");
        xmlHttp.send('');
        return xmlHttp.getResponseHeader("Date");
    }else if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
        xmlHttp.open('HEAD',window.location.href.toString(),false);
        xmlHttp.setRequestHeader("Content-Type", "text/html");
        xmlHttp.send('');
        return xmlHttp.getResponseHeader("Date");
    }
}

     var st = srvTime();
     var date = new Date(st);
     var today = date.getFullYear() + addZero(date.getMonth()+1) + addZero(date.getDate());

 

      function addZero(n){
          return n < 10 ? "0" + n : n;
      }

 

반응형

관련글 더보기