자바 스크립트에서 간단한 추가 / 빼기 시간 / 분 기능을 사용하려면 다음을 시도하십시오.
function getTime (addHour, addMin){
addHour = (addHour?addHour:0);
addMin = (addMin?addMin:0);
var time = new Date(new Date().getTime());
var AM = true;
var ndble = 0;
var hours, newHour, overHour, newMin, overMin;
//change form 24 to 12 hour clock
if(time.getHours() >= 13){
hours = time.getHours() - 12;
AM = (hours>=12?true:false);
}else{
hours = time.getHours();
AM = (hours>=12?false:true);
}
//get the current minutes
var minutes = time.getMinutes();
// set minute
if((minutes+addMin) >= 60 || (minutes+addMin)<0){
overMin = (minutes+addMin)%60;
overHour = Math.floor((minutes+addMin-Math.abs(overMin))/60);
if(overMin<0){
overMin = overMin+60;
overHour = overHour-Math.floor(overMin/60);
}
newMin = String((overMin<10?'0':'')+overMin);
addHour = addHour+overHour;
}else{
newMin = minutes+addMin;
newMin = String((newMin<10?'0':'')+newMin);
}
//set hour
if(( hours+addHour>=13 )||( hours+addHour<=0 )){
overHour = (hours+addHour)%12;
ndble = Math.floor(Math.abs((hours+addHour)/12));
if(overHour<=0){
newHour = overHour+12;
if(overHour == 0){
ndble++;
}
}else{
if(overHour ==0 ){
newHour = 12;
ndble++;
}else{
ndble++;
newHour = overHour;
}
}
newHour = (newHour<10?'0':'')+String(newHour);
AM = ((ndble+1)%2===0)?AM:!AM;
}else{
AM = (hours+addHour==12?!AM:AM);
newHour = String((Number(hours)+addHour<10?'0':'')+(hours+addHour));
}
var am = (AM)?'AM':'PM';
return new Array(newHour, newMin, am);
};
현재 시간을 얻기 위해 매개 변수없이 사용할 수 있습니다
getTime();
또는 분 / 시간이 추가 된 시간을 얻기위한 매개 변수
getTime(1,30); // adds 1.5 hours to current time
getTime(2); // adds 2 hours to current time
getTime(0,120); // same as above
부정적인 시간조차도
getTime(-1, -30); // subtracts 1.5 hours from current time
이 함수는
array([Hour], [Minute], [Meridian])