서버시간과 클라이언트시간 똑같이 맞추기
var today = new Date();
var dd = set_two_number(today.getDate()+"");
var mm = set_two_number((today.getMonth()+1)+""); //January is 0!
var yyyy = today.getFullYear()+"";
var hours = set_two_number(today.getHours());
var minutes = set_two_number(today.getMinutes());
var secodes = set_two_number(today.getSeconds());
var ymdhis_client = yyyy+mm+dd+hours+minutes+secodes;
var ymdhis_server = "<?=date('YmdHis')?>";
//console.log("client => " + ymdhis_client + " / server => " + ymdhis_server);
result = get_diff_time(ymdhis_client, ymdhis_server);
//console.log(result);
time_interval = result['buho']+result['diffSec'];
time_interval = parseInt(time_interval);
var currentDate = new Date();
currentDate = currentDate.addSeconds(time_interval);
////////////////////////////// 시간차 계산
function get_diff_time(startTime, endTime)
{
//var startTime = "20090101123000"; // 시작일시 ('2009-01-01 12:30:00')
//var endTime = "20091001172010"; // 종료일시 ('2009-10-01 17:20:10')
var buho = "";
if(endTime > startTime)
{
buho = "+";
}
else
{
buho = "-";
var tmpTime = startTime;
startTime = endTime;
endTime = tmpTime;
}
//console.log(startTime + "/" +endTime);
// 시작일시
var startDate = new Date(parseInt(startTime.substring(0,4), 10),
parseInt(startTime.substring(4,6), 10)-1,
parseInt(startTime.substring(6,8), 10),
parseInt(startTime.substring(8,10), 10),
parseInt(startTime.substring(10,12), 10),
parseInt(startTime.substring(12,14), 10)
);
// 종료일시
var endDate = new Date(parseInt(endTime.substring(0,4), 10),
parseInt(endTime.substring(4,6), 10)-1,
parseInt(endTime.substring(6,8), 10),
parseInt(endTime.substring(8,10), 10),
parseInt(endTime.substring(10,12), 10),
parseInt(endTime.substring(12,14), 10)
);
//console.log(startDate);
//console.log(endDate);
// 두 일자(startTime, endTime) 사이의 차이를 구한다.
var dateGap = endDate.getTime() - startDate.getTime();
var timeGap = new Date(0, 0, 0, 0, 0, 0, endDate - startDate);
// 두 일자(startTime, endTime) 사이의 간격을 "일-시간-분"으로 표시한다.
var diffDay = Math.floor(dateGap / (1000 * 60 * 60 * 24)); // 일수
var diffHour = timeGap.getHours(); // 시간
var diffMin = timeGap.getMinutes(); // 분
var diffSec = timeGap.getSeconds(); // 초
// 출력 : 샘플데이타의 경우 "273일 4시간 50분 10초"가 출력된다.
// alert(diffDay + "일 " + diffHour + "시간 " + diffMin + "분 " + diffSec + "초
var result = new Array();
result['buho'] = buho;
result['diffDay'] = diffDay+"";
result['diffHour'] = diffHour+"";
result['diffMin'] = diffMin+"";
result['diffSec'] = diffSec+"";
return result;
}
Date.prototype.addSeconds = function(seconds) {
this.setSeconds(this.getSeconds() + seconds);
return this;
};
Date.prototype.addMinutes = function(minutes) {
this.setMinutes(this.getMinutes() + minutes);
return this;
};
Date.prototype.addHours = function(hours) {
this.setHours(this.getHours() + hours);
return this;
};
Date.prototype.addDays = function(days) {
this.setDate(this.getDate() + days);
return this;
};
Date.prototype.addWeeks = function(weeks) {
this.addDays(weeks*7);
return this;
};
Date.prototype.addMonths = function (months) {
var dt = this.getDate();
this.setMonth(this.getMonth() + months);
var currDt = this.getDate();
if (dt !== currDt) {
this.addDays(-currDt);
}
return this;
};
Date.prototype.addYears = function(years) {
var dt = this.getDate();
this.setFullYear(this.getFullYear() + years);
var currDt = this.getDate();
if (dt !== currDt) {
this.addDays(-currDt);
}
return this;
};
function set_two_number(number)
{
number = number + "";
number = number.length == 1 ? "0" + number : number;
return number;
}