슬기로운생활/Web/Servlet

javascript(Ajax) 서버 시간 가져오기

좀모씨 2014. 12. 17. 16:15


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;

}